Try it first — give it 10 minutes
0/4 revealedStuck? Take one hint at a time. The struggle is where the pattern sticks.
The problem in plain English
Given an array nums, return true if some value shows up two or more times, and false if every value is unique.
[1, 2, 3, 1]→true(1 appears twice)[1, 2, 3, 4]→false
Start with brute force
Compare every pair (i, j) with i < j. If any pair is equal, return true. That's O(n²) comparisons — too slow when n is 100,000.
Building the intuition
There are two classic ways to beat O(n²), and you should know both:
- Sort first. After sorting, equal values sit next to each other, so you only compare neighbours. O(n log n) time, and O(1) extra space if you sort in place.
- Remember what you've seen. Keep a hash set. For each number, ask "is it already in the set?" That's O(1) per number, so O(n) total.
Interviewers love hearing both, because it shows you understand the time vs. space trade-off: sorting saves memory, the set saves time.
The optimal approach
- Create an empty hash set
seen. - For each number
x:- If
xis inseen, returntrue— that's a duplicate. - Otherwise add
xtoseen.
- If
- If the loop ends, every number was unique: return
false.
The code
Every solution below is tested against the examples and edge cases before it ships. Pick your language — we'll remember it.
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
seen = set()
for x in nums:
if x in seen: # already met this value -> duplicate
return True
seen.add(x)
return False # every value was uniqueclass Solution {
public:
bool containsDuplicate(vector<int>& nums) {
unordered_set<int> seen;
for (int x : nums) {
if (seen.count(x)) { // already met this value -> duplicate
return true;
}
seen.insert(x);
}
return false; // every value was unique
}
};class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> seen = new HashSet<>();
for (int x : nums) {
// add() returns false when x was already in the set
if (!seen.add(x)) {
return true;
}
}
return false; // every value was unique
}
}/**
* @param {number[]} nums
* @return {boolean}
*/
var containsDuplicate = function (nums) {
const seen = new Set();
for (const x of nums) {
if (seen.has(x)) return true; // already met this value -> duplicate
seen.add(x);
}
return false; // every value was unique
};Dry run
nums = [1, 2, 3, 1]
| x | seen before | In set? | Action |
|---|---|---|---|
| 1 | {} | No | add 1 |
| 2 | {1} | No | add 2 |
| 3 | {1, 2} | No | add 3 |
| 1 | {1, 2, 3} | Yes | return true |
Complexity
- Time: O(n). Each lookup and insert is O(1) on average.
- Space: O(n). The set can hold every element if they're all unique.
The sorting alternative is O(n log n) time, O(1) extra space (ignoring the sort's own stack).
Edge cases and traps
- One element. Can't have a duplicate:
false. - Negative numbers and zero. A set handles them like any other value.
- Returning early. Return
truethe moment you find a repeat — no need to finish the loop. - Python one-liner.
len(set(nums)) != len(nums)is correct and fine to mention, but it always processes the whole array; the loop stops early.
Say it in the interview
"Brute force compares every pair, O(n²). I can sort and compare neighbours in O(n log n) with no extra memory, or use a hash set: for each number, if it's already in the set I return true, otherwise I add it. That's O(n) time and O(n) space. I'd go with the set unless memory is tight."
Follow-ups interviewers ask
- "Duplicates within distance k?" (Contains Duplicate II) Keep a map of value → last index, or a sliding-window set of size
k. - "What if you can't use extra memory?" Sort in place and compare neighbours.
- "What if the values are in a small range, say 0 to 1000?" Use a boolean array instead of a set — same idea, faster in practice.
Practise the same idea
This is Day 10 of a free 90-day plan 🐸
90 problems in the order that builds on itself, one a day, with a walkthrough like this for each. Tick them off and keep your streak.
Get the full roadmap — free