Find the Duplicate Number - Problem
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 in nums, return this repeated number.
You must solve the problem without modifying the array nums and using only constant extra space.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,3,4,3,2]
›
Output:
3
💡 Note:
The number 3 appears at indices 1 and 3, making it the duplicate number
Example 2 — Duplicate at End
$
Input:
nums = [3,1,3,4,2]
›
Output:
3
💡 Note:
The number 3 appears at indices 0 and 2, so 3 is the duplicate
Example 3 — Minimum Size
$
Input:
nums = [1,1]
›
Output:
1
💡 Note:
With only 2 elements in range [1,1], the duplicate must be 1
Constraints
- 1 ≤ n ≤ 105
- nums.length == n + 1
- 1 ≤ nums[i] ≤ n
- All integers in nums appear only once except for precisely one integer which appears two or more times.
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array with n+1 integers, one appears twice
2
Process
Find the number that appears more than once
3
Output
Return the duplicate number
Key Takeaway
🎯 Key Insight: Transform into cycle detection by treating array values as pointers to indices
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code