Contains Duplicate
Find out whether any number appears twice by remembering every number you've already seen.
01The problem
You get an array of integers nums. Return true if any value appears at least twice, and false if every value is different.
| Input | Output | Why |
|---|---|---|
[1, 2, 3, 1] | true | 1 appears twice |
[1, 2, 3, 4] | false | all different |
[] | false | nothing to repeat |
Constraints: up to 100,000 numbers, each between −10⁹ and 10⁹.
02How to spot it
The question is literally "have I seen this before?" That's the signal for a HashSet: one pass, asking the set before adding to it.
03Pseudocode
seen = empty set
for each n in nums:
if n is in seen: return true
add n to seen
return false- seen holds every number to the left of where we are.
- If the current number is already in seen, it appeared earlier: that's a duplicate.
- If we finish without a match, all numbers were different.
04Watch it work
seen
Start with an empty set called seen. It will remember every number we pass.
Pseudocode
- seen = empty set
- for each n in nums:
- if n is in seen: return true
- add n to seen
- return false
Variables
- i
- –
- n
- –
05Step-by-step dry run
- 110213213
seen
emptyStart with an empty set called seen. It will remember every number we pass.
i = –n = –
- 210213213i
seen
11 isn't in seen yet, so it's new. Add it and move on.
i = 0n = 1
- 310213213i
seen
122 isn't in seen yet, so it's new. Add it and move on.
i = 1n = 2
- 410213213i
seen
1233 isn't in seen yet, so it's new. Add it and move on.
i = 2n = 3
- 510213213i
seen
1231 is already in seen: we met it at index 0. That's a duplicate, so return true straight away.
i = 3n = 1
06Java solution
import java.util.HashSet;
import java.util.Set;
public class ContainsDuplicate {
static boolean containsDuplicate(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int n : nums) {
if (!seen.add(n)) return true; // add returns false when n was already there
}
return false;
}
public static void main(String[] args) {
System.out.println(containsDuplicate(new int[] {1, 2, 3, 1}));
System.out.println(containsDuplicate(new int[] {1, 2, 3, 4}));
System.out.println(containsDuplicate(new int[] {}));
}
}true false false
The tricky line: seen.add(n) both checks and adds. It returns false when n was already in the set, so one call replaces contains plus add.
07Why it works
Before looking at index i, the set holds exactly nums[0..i−1]. So "is nums[i] in the set?" is the same as "did nums[i] appear earlier?" The first time the answer is yes, we've found a duplicate.
08Time and space
- Time: O(n). One pass; each set operation is O(1) on average.
- Space: O(n). In the worst case (no duplicates) the set holds every number.
09Edge cases
| Case | Result |
|---|---|
| Empty array | false |
| One element | false |
All the same [7,7,7] | true at index 1 |
| Negative numbers | Work the same way |
10Common mistakes
- Two nested loops. Correct but O(n²): too slow for 100,000 numbers.
- Adding before checking. Then every number "is in the set" and you always return
true. - Using
List.contains. A list search is O(n), so the whole thing becomes O(n²) again.
11Other approaches
| Approach | Time | Extra space | Note |
|---|---|---|---|
| Compare every pair | O(n²) | O(1) | Too slow for big inputs |
| HashSet | O(n) | O(n) | Best general answer |
| Sort, check neighbours | O(n log n) | O(1) | Changes the array; good if memory is tight |
12In the interview
Common follow-ups:
- Duplicates within k positions? Keep only the last k numbers in the set (Contains Duplicate II).
- Return the duplicate instead? Return
ninstead oftrue.
13Check your understanding
Quiz · 3 questions
0 of 3 answered
1. Why is a HashSet better than a List for "seen"?
2. What does seen.add(5) return if 5 is already in the set?
3. What is the space cost of the HashSet solution?
14Solve it yourself
Try it on LeetCode #217 without looking back at this page.