Find the Duplicate Number
Master this topic with zero to advance depth.
Find the Duplicate Number
Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive, there is only one repeated number, return this repeated number. You must solve the problem without modifying the array nums and use only constant extra space.
Array: [1, 3, 4, 2, 2]
Index: 0 1 2 3 4
Treat values as pointers:
0 -> 1 -> 3 -> 2 -> 4 -> 2... (Cycle!)
^----|Examples
Level I: Sorting
Intuition
If we sort the array, the duplicate number will be adjacent to itself. This modifies the input array, which is usually not ideal, but it's a simple first approach.
Detailed Dry Run
Input: [1, 3, 4, 2, 2]
- Sorted:
[1, 2, 2, 3, 4] nums[1] == nums[2]? YES. Return 2.
⚠️ Common Pitfalls & Tips
Modifies the input array, which may violate problem constraints.
Level II: HashSet / Visited Array
Intuition
As we iterate through the array, keep track of numbers we've seen. If we see a number again, it's the duplicate.
Detailed Dry Run
Input: [1, 3, 4, 2, 2]
- Seen:
{1, 3, 4, 2} - Next is 2.
2is in Seen. Return 2.
⚠️ Common Pitfalls & Tips
Uses O(N) space, violating the constant space constraint.
Level III: Floyd's Cycle Finding (Tortoise and Hare)
Intuition
Since the array contains values in the range [1, n] and has n+1 elements, we can treat the array values as pointers to indices. A duplicate value will cause multiple indices to point to the same next index, creating a cycle. We can use Floyd's algorithm to find the entrance of this cycle, which is our duplicate number.
- Find Intersection: Use a
slow(1 step) andfast(2 steps) pointer. Move them until they meet in a cycle. - Find Entrance: Reset another pointer to the start of the array. Move both pointers one step at a time. The point where they meet is the start of the cycle (the duplicate number).
Detailed Dry Run
Input: [1, 3, 4, 2, 2]
indices: 0->1->2->3->4
values: 1, 3, 4, 2, 2
Linked List: 0 -> 1 -> 3 -> 2 -> 4 -> 2...
- Phase 1:
slowat 1,fastat 3. Thenslowat 3,fastat 4. Thenslowat 2,fastat 2. MEET! - Phase 2:
p1=0,p2=2(meeting point).
- Loop 1:
p1 = nums[0] = 1, p2 = nums[2] = 4 - Loop 2:
p1 = nums[1] = 3, p2 = nums[4] = 2 - Loop 3:
p1 = nums[3] = 2, p2 = nums[2] = 4... Meeting at 2.
⚠️ Common Pitfalls & Tips
This only works if the array values are in the range [1, n]. If there's a 0, the pointer logic might get stuck at index 0.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.