Understand
30-second explanation
Build the interview-ready mental model before comparing implementations.
Walk the array adding each value to a hash set; the first value that's already present is a duplicate. O(n) time, O(n) space.
Interview contextWhat is testedWhere candidates slipHow to stand outExplore interview contextHide interview context
What's being tested
Whether you reach for a hash set to trade space for time, with correct ordering.
Where candidates slip
O(n²) pair comparison, or add-before-check.
How to stand out
Offer the O(1)-space variants when values are bounded to 1..n (sign marking / Floyd).
Choose
Compare the available approaches
Use constraints and trade-offs to select the route you can defend in an interview.
Learn & trace
Build each solution step by step
Move from the core idea to a visual trace, implementation, proof, and edge cases.
Approach 1 of 1 · Optimal
Hash set seen
1Hint 1Explore hintHide hint
What data structure answers 'seen this before?' in O(1)?
2Hint 2Explore hintHide hint
Add elements to a set; the first failed insert is your duplicate.
3Hint 3Explore hintHide hint
Check membership before inserting, not after.
A · Core idea
Use a hash set as a memory of what you've already seen. Walk the array; the first element that is already in the set is a duplicate. O(n) time, O(n) space.
Why a set. A set answers 'have I seen this value before?' in O(1) average time. That converts the O(n²) brute force (compare every pair) into a single linear pass — you trade O(n) memory for the speedup.
Check-then-add ordering. You must test membership before inserting; insert first and the value is always 'present', so you'd never detect the repeat. In Java the idiom if (!seen.add(x)) return x; is neat because add returns false when the element already existed.
Constrained variants. If values are guaranteed in 1..n, you can find a duplicate in O(1) space using index-sign marking or Floyd's cycle detection — good things to mention, since the set solution uses extra memory.
B · Visual dry run
Interactive dry run
Select a frame to connect the decision with its visual state.
nums = [1, 3, 2, 3, 4]1
not in set
{1}3C · Code with teaching comments
D · Why it works and complexityO(n) timeO(n) spaceOpen the invariant and proofExplore the proofHide the proof
Mention the O(n²) nested-loop brute force first, then the O(n) set — the trade is time for O(n) space.
ComplexityO(n) time, O(n) space — each element does an O(1) set lookup/insert, so O(n) time; the set can hold up to n values, so O(n) space.
E · Edge cases and pitfalls4nums = [5, 5] — Immediate repeat → 5.Explore edge casesHide edge cases
Edge cases handled
nums = [5, 5]Immediate repeat → 5.nums = [1, 2, 2, 1]Returns the first repeat encountered (2).
Pitfalls
- Nested loops (O(n²)) when a set gives O(n).
- Inserting before checking, so the repeat is never detected.
Practice
Try the solution yourself
Edit the optimal implementation, test another input, and observe the result.
Editable code previewpublic int findDuplicate(int[] nums) { java.util.Set<Integer> seen = new java.util.HashSet<>(); for (int x : nums) { if (!seen.add(x)) return x; }Explore the playgroundClose the playground
Avoid mistakes
Recognize the interview traps
Understand why common attempts fail before reviewing the corrected implementation.
1Add before checking
If you insert the value first, the membership test always succeeds and no duplicate is ever reported.
Explore the code comparisonHide the code comparison
Add before checking
If you insert the value first, the membership test always succeeds and no duplicate is ever reported.
Don't write this
for x in nums:
seen.add(x)
if x in seen: return x # always trueWrite this instead
for x in nums:
if x in seen: return x
seen.add(x)Revise
Carry the pattern into the interview
Pattern to remember
Hash set membership
Code pattern to memorize
seen=set(); for x: if x in seen: return x; seen.add(x)Rules to remember
- 1Add as you go.
- 2Already-present ⟹ duplicate.
- 3O(n) time, O(n) space.
Use this pattern when
- Detecting repeats
- 'Have I seen this?' membership
Choose another pattern when
- Values bounded to 1..n and O(1) space required — use sign marking
Interview takeaway
'Seen before?' questions are a hash set in disguise.
Continue
Transfer the pattern to another problem
Use a direct variation or pattern sibling to prove the idea is reusable.
Transferable patternA core fresher pattern: Hash set membership. Master it on small inputs and it transfers to harder problems.
Practice queueDirect variations and pattern siblings
Direct follow-ups
- Contains DuplicateComing soonReturn just true/false using the same set.
- Find the Duplicate Number (O(1) space)Coming soonFloyd's cycle detection when values are in 1..n.