Algorithms and Data Structures · Junior · Question 17 of 75

How do you check in O(n) whether an array contains duplicates?

Explanation

One pass with a Set: if seen.has(x) is already true, there's a duplicate. Nested loops and indexOf inside a loop are both O(n²).

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 17

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