Contains Duplicate
Given an integer array `nums`, return `true` if any value appears at least twice in the array, and return `false` if every element is distinct. **Examples:** ```typescript containsDuplicate([1, 2, 3, 1]) // true containsDuplicate([1, 2, 3, 4]) // false containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2]) // true ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(n)
- Space complexity: O(n)
Single pass over nums with O(1) hash set lookups and insertions.
Follow-Up Questions Interviewers Ask About Contains Duplicate
- How to solve this with O(1) space?
Sort the array first in O(n log n) time and check adjacent elements.
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.