287. Find the Duplicate Number
Asked at Walmart
Difficulty
Medium
Time
O(n)
Space
O(1)
Pattern
Array
Companies
1
Problem
You are given an array nums of length n + 1. Every value in it is an integer between 1 and n. Exactly one value appears more than once (it may appear two or more times); every other value appears once.
Return that repeated value.
Two rules make this harder than it looks: you must not modify the array, and you may use only constant extra space (no hash set, no copy of the array).
Values point to indices
Constraints
- ●
1 <= n <= 10^5 - ●
nums.length == n + 1 - ●
1 <= nums[i] <= n - ●
Every value appears once except exactly one value, which appears two or more times
Examples
nums = [1,3,4,2,2]2nums = [3,1,3,4,2]3nums = [3,3,3,3,3]3Asked At
1 companies| Company | Difficulty | |
|---|---|---|
| Walmart | Medium | View all Walmart questions → |
How to Think About It
Brute force: For each element, scan all other elements to find a match — O(n²) time.
Sorting approach: Sort and check adjacent elements — O(n log n) time but violates the no-modify constraint.
Binary search on value range: Count elements ≤ mid to determine which half contains the duplicate.
Bit manipulation: Count set bits at each position across all numbers and compare with expected counts from 1..n.
Optimal: Floyd's tortoise and hare cycle detection — treat array values as pointers to find the cycle entrance.
Optimal Approach
Treat the array as a linked list where the value at index i is the next node index. Since a duplicate exists, two indices point to the same node, creating a cycle. Phase 1: Move slow one step and fast two steps until they meet inside the cycle. Phase 2: Reset slow to index 0 and move both one step at a time — their meeting point at the cycle entrance is the duplicate. This runs in O(n) time and O(1) space.
Floyd's tortoise and hare
What Trips People Up in Real Interviews
Ask whether you can modify the array — the follow-up is always the constant-space version.
Explain the Pigeonhole Principle: n+1 values in range [1,n] guarantees a duplicate.
Map the array to a linked list: index i points to nums[i], creating a cycle with the duplicate as entrance.
Implement two-phase cycle detection: slow/fast meeting point, then restart slow from index 0.
Walk through [1,3,4,2,2] to show the cycle: 0→1→3→2→4→2→4...
Solution Code
O(n) timeclass Solution:
def findDuplicate(self, nums):
slow = nums[0]
fast = nums[nums[0]]
while slow != fast:
slow = nums[slow]
fast = nums[nums[fast]]
slow = 0
while slow != fast:
slow = nums[slow]
fast = nums[fast]
return slowFrequently Asked Questions
What is the Find the Duplicate Number problem?
Given an array of n+1 integers where each integer is between 1 and n inclusive, find the one duplicate number without modifying the array and using only constant extra space. The duplicate may appear more than once but there is guaranteed to be exactly one repeated value.
How do you solve Find the Duplicate Number?
The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Find the Duplicate Number?
Find the Duplicate Number is asked at Walmart. It is a medium difficulty problem.
What are common mistakes on Find the Duplicate Number?
- Ask whether you can modify the array — the follow-up is always the constant-space version.
- Explain the Pigeonhole Principle: n+1 values in range [1,n] guarantees a duplicate.
- Map the array to a linked list: index i points to nums[i], creating a cycle with the duplicate as entrance.
- Implement two-phase cycle detection: slow/fast meeting point, then restart slow from index 0.
- Walk through [1,3,4,2,2] to show the cycle: 0→1→3→2→4→2→4...