Algorithms and Data Structures · Junior · Question 8 of 75
What is the complexity of arr.includes(x) on an unsorted array?
- 1O(log n)
- ✓Correct answer: O(n)
- 3O(1)
Explanation
includes compares elements one by one until it finds a match, so in the worst case it checks all n. If you need many membership checks, build a Set once and use set.has(x), which is O(1) on average.