Contains Duplicate
Walk the array once and keep every value you have passed in an unordered_set. Before adding x, the set already tells you in O(1) whether x was seen. If it was, you found a duplicate.
nums[i] is a duplicate exactly when it is already in seen — that means it appeared at some earlier index.
Problem
You are given an integer array nums.
Return true if some value appears at least twice in the array. Return false if every value is different.
An integer array nums.
true if any value repeats, otherwise false.
The value 1 appears at index 0 and again at index 3.
All four values are different.
Several values repeat. Finding one repeat is enough.
- • 1 ≤ nums.length ≤ 10^5
- • −10^9 ≤ nums[i] ≤ 10^9
First, think yourself
Before opening any solution, try to answer these on paper. Use hints only when stuck — each hint is counted.
- 1.
Do I need to know which value repeats, or how many times, or only whether one repeats?
- 2.
Does it matter how far apart the two equal values are?
- 3.
If I walk left to right, what do I need to remember about the values I have already passed?
- 4.
If the array were sorted, where would two equal values be?
- 5.
Am I allowed to change the array? Do I care about extra memory?
Brute force approach
Better approach
Optimized approach
Why does this algorithm work?
Dry run
All approaches compared
| Approach | Idea | Time | Space |
|---|---|---|---|
| Brute Force | Check every pair (i, j) for equal values | O(N²) | O(1) |
| Better | Sort; equal values become neighbours | O(N log N) | O(1) |
| Optimal | Insert each value into a set; a failed insert means a duplicate | O(N) | O(N) |
Why does this belong to this pattern?
- “appears at least twice”We only need "have I seen this before?" — a membership question.
- “return true / false”No counts or positions needed → a set is enough, not a map.
- “any two positions (no distance limit)”Remember **every** earlier value, not just a window of recent ones.
This is the anchor of the set-membership sub-pattern: put values in a set, and each "is x present?" question costs O(1). Longest Consecutive Sequence asks the same question about x − 1 and x + 1 instead of x itself, and First Missing Positive asks it about 1, 2, 3, …. Contains Duplicate II adds a distance limit k, which turns the set into a sliding window of the last k values — this problem has no such limit.
Don't make this mistake
This is a pattern-confusion trap. Contains Duplicate II only counts duplicates at most k apart, so it keeps a window of the last k values. Here there is no distance limit: two equal values at index 0 and index 99999 still count. Erasing old values makes you miss them.
Counts are not needed — only "seen or not". The map version also always reads the whole array and then loops again, instead of stopping at the first repeat. Pick the smallest structure that answers the question.
The neighbour check is only correct because sorting puts equal values together. On the unsorted [1, 2, 3, 1] no two neighbours are equal, so it wrongly returns false.
With j = 0, the pair i == j compares an element with itself, so every array looks like it has a duplicate.
Want to implement this?
Write it yourself on LeetCode, then come back and compare your code with the solutions above.
Open coding workspaceHow this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Arrays & Hashing translate into production architectures and low-level memory mechanics.