Practice Contains Duplicate
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
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.
Source: Contains Duplicate on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
nums = [1,2,3,1]
true
# Example 2
nums = [1,2,3,4]
false
# Example 3
nums = [1,1,1,3,3,4,3,2,4,2]
true
Constraints
How to solve Contains Duplicate
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
A hash set answers whether a value has been seen before in constant expected time, so a single pass that inserts each value and stops at the first repeat settles the question without comparing pairs.
Why brute force is not enough
Comparing every pair of indices is O(n^2), around 5 * 10^9 comparisons for n up to 10^5. Sorting and checking neighbors is O(n log n), acceptable but slower than hashing, and it reorders or copies the input.
Approach
- Create an empty hash set; the invariant is that it contains exactly the distinct values from the prefix processed so far.
- Walk the array once, and for each value check membership; if it is present, return true immediately.
- Otherwise insert the value and move on to the next element.
- If the loop completes, every value was distinct, so return false.
- If memory is the tighter constraint, sort in place and return true the first time two adjacent elements are equal.
Walkthrough of Example 1
nums = [1,2,3,1] → true
- Start with an empty set; 1 is absent, so insert it, giving {1}.
- 2 is absent, insert it, giving {1, 2}.
- 3 is absent, insert it, giving {1, 2, 3}.
- The final 1 is already present, so return true without examining anything further.
Time and space
Time O(n) · Space O(n). n is nums.length; each element costs one expected constant-time lookup and insertion, and with no duplicates the set grows to all n values.
Interview pitfalls
- Comparing the set's size to the array length only after inserting everything; correct, but it forfeits the early exit when a duplicate appears near the front.
- Trying a boolean array indexed by value; values span -10^9 to 10^9, so a real hash set or sorting is required.
- In the sorting variant, comparing elements two apart or forgetting that duplicates sit adjacent only after sorting.
- Treating example 3, where values appear three or four times, as needing counts; the first repeat is sufficient.
Follow-up questions interviewers ask
- How would you detect a duplicate only within any window of k indices, as in Contains Duplicate II?
- If the array does not fit in memory, how would you check for duplicates with external sorting or partitioned hashing?
- How would you return the first repeated value instead of a boolean?
Further reading on the technique: Hash table
Related LeetCode Problems
Practice more problems similar to Contains Duplicate.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.