Contains Duplicate — Coding Interview Walkthrough
You’re in a coding interview. The interviewer says: “Given an integer array, return true if any value appears at least twice.” Contains Duplicate looks trivial. It’s LeetCode #217, and it appears in nearly every screening round — not because the code is hard, but because the conversation is. How many approaches do you know? Can you compare them? Can you explain the trade-off between time and space without being prompted? That’s what this problem is actually testing.
The Problem
Given an integer array nums, return true if any value appears at least twice in the array, and false if every element is distinct.
Example 1
Input
nums = [1, 2, 3, 1]
Output
true
Example 2
Input
nums = [1, 2, 3, 4]
Output
false
Example 3
Input
nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]
Output
true
Already comfortable with the solution? Practice it in a mock interview →
What Interviewers Are Actually Testing
| Skill | What Interviewers Observe |
|---|---|
| Hash set intuition | Do you immediately recognize this as an O(1) lookup problem? |
| Trade-off articulation | Can you compare sorting vs. hash set and choose deliberately? |
| Early exit | Do you return as soon as a duplicate is found, or scan the entire array? |
| Edge case awareness | Empty arrays, single-element arrays, large inputs with all duplicates |
| Follow-up readiness | Can you extend this to “find duplicate within k distance”? |
Step 1: Clarifying Questions
1. Can the array be empty or have one element?
Both return false — no duplicate possible. Confirms you handle base cases.
2. Are the integers bounded? If values fit in a limited range, you might use a boolean array instead of a hash set. Usually not relevant for this problem but shows range-based thinking.
3. Can values be negative? Yes. A hash set handles this naturally; a fixed array approach would not.
4. Is there a space constraint? This opens the sorting alternative (O(1) extra space vs. O(n) for the set).
Step 2: A First (Non-Optimal) Idea
Compare every pair:
def containsDuplicate_brute(nums: list[int]) -> bool:
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] == nums[j]:
return True
return False
O(n²) time, O(1) space. Correct but too slow for large inputs.
Step 3: The Key Insight
Duplicate detection is a membership problem. The question “have I seen this number before?” is answered in O(1) by a hash set. Add each number as you scan — if it’s already in the set, you’ve found a duplicate. This gives O(n) time with O(n) space, which is the standard trade-off for fast lookup.
Step 4: Optimal Strategy
Option A — Hash Set (O(n) time, O(n) space):
- Initialize an empty set.
- For each number, check if it’s already in the set.
- If yes, return
True. If no, add it. - Return
Falseafter the loop.
Option B — Sort first (O(n log n) time, O(1) space):
- Sort the array.
- Scan adjacent pairs — if any are equal, return
True. - Return
False.
Python Implementation
def containsDuplicate(nums: list[int]) -> bool:
seen = set()
for num in nums:
if num in seen:
return True
seen.add(num)
return False
One-liner alternative:
def containsDuplicate(nums: list[int]) -> bool:
return len(nums) != len(set(nums))
The one-liner is clean but always processes the full array. The loop version returns early on first duplicate found — better in practice when duplicates appear early.
Sorting approach (O(1) extra space):
def containsDuplicate(nums: list[int]) -> bool:
nums.sort()
for i in range(1, len(nums)):
if nums[i] == nums[i - 1]:
return True
return False
Note: this modifies the input array — worth flagging in an interview.
Time & Space Complexity
| Approach | Time | Space |
|---|---|---|
| Brute force | O(n²) | O(1) |
| Hash set | O(n) | O(n) |
| Sort | O(n log n) | O(1) extra |
Common Interview Mistakes
-
Using the brute force without recognizing it’s O(n²). Always state complexity before the interviewer asks.
-
Using the one-liner without mentioning early exit.
set(nums)builds the full set regardless. If a duplicate appears at index 1 of a million-element array, the loop is vastly faster. -
Sorting without noting it modifies the input. In-place sort changes the caller’s data — worth mentioning even if it’s acceptable here.
-
Not discussing the time/space trade-off. The interviewer almost always wants to hear you compare approaches. “I’ll use a hash set for O(n) time at the cost of O(n) space. If space is constrained, sorting gives O(n log n) with O(1) extra” is a model answer.
-
Not asking about the follow-up. Contains Duplicate II (within k distance) is a natural follow-up. Showing awareness of it signals interviewer-level problem thinking.
What a Strong Candidate Sounds Like
“I’ll use a hash set. As I scan the array, I check if the current element is already in the set — if yes, return true. If no, add it and continue. That’s O(n) time and O(n) space. If space is a concern, I could sort first and check adjacent pairs — O(n log n) time but only O(1) extra space. I’ll go with the hash set since speed is usually preferred unless told otherwise.”
Example Interview Dialogue
Interviewer: What if I wanted to know if any duplicate appears within k positions of another?
Candidate: That’s Contains Duplicate II. I’d use a sliding window hash set of size k. As I scan, I add each element to the set and remove the element k positions before the current index. If the current element is already in the set, it’s within k distance — return true. Same O(n) time, O(k) space.
Interviewer: What does len(nums) != len(set(nums)) do?
Candidate: It converts the array to a set — which drops duplicates — then compares sizes. If the set is smaller, a duplicate existed. It’s concise but always scans the whole array. My loop version returns early, which is better when duplicates are common or the array is large.
Related Problems to Practice
- Contains Duplicate II (LeetCode #219). Find duplicates within k index distance — sliding window extension.
- Contains Duplicate III (LeetCode #220). Duplicates within k distance AND value difference within t — sorted set approach.
- Find the Duplicate Number (LeetCode #287). Find the duplicate in an array where you can’t use extra space — Floyd’s cycle detection.
- Happy Number (LeetCode #202). Uses a set to detect cycles — same membership-check pattern.
From the Intervu walkthrough collection:
- Valid Anagram — Hash-based character counting.
- Two Sum — Hash map for O(1) lookups.
- Majority Element — Array frequency analysis.
Practice This in a Mock Interview
Contains Duplicate is a problem where the code is easy but the conversation is what matters. Interviewers want to hear you name multiple approaches, compare trade-offs, and choose deliberately. That fluency only comes from practice.
Start a mock interview for Contains Duplicate on Intervu.
Further reading:
- How to Prepare for a Coding Interview in 2026, the complete roadmap and study plans
- Why LeetCode alone isn’t enough, and what to practice instead
- Practice any LeetCode problem as a live mock interview
- The Grind 75 Pathway, a structured study plan with AI practice links
- Browse all walkthroughs on GitHub, condensed solutions with code