Algorithms and Data Structures · Junior · Question 8 of 75

What is the complexity of arr.includes(x) on an unsorted array?

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.

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 8

Test yourself

Take the Algorithms and Data Structures · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz