Find the Duplicate Number
Find the duplicate number in an array of n+1 integers in range [1,n] Use O(1) space and don't modify the array Example: [1,3,4,2,2] => 2
Find the Duplicate Number rewards precision: narrowing the search space correctly is the whole game. Interviewers love it because it reveals your boundary precision and your edge-case discipline. Interviewers care more about airtight boundaries than speed here.
Examples
You write
find_duplicate(nums: List[int])
Example 1
- Input
- [1,3,4,2,2]
- returns
- 2
Example 2
- Input
- [3,1,3,4,2]
- returns
- 3
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problemsTwo Pointers
I reach for it when
- The array is sorted, or you can sort it.
- You need a pair, a triple or a palindrome.
Say this before you type. Say what each pointer means and what a move to the left or right rules out.
All 26 Two Pointers problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.
Prep guides that list this problem
Our company tags come from public interview write-ups. None of these is a confirmed question.