Find the Duplicate Number
Difficulty: Medium
Given an array of n + 1 integers, each between 1 and n (inclusive), exactly one value repeats (two or more times). Find it.
Don't modify the array, and use only constant extra memory. Sorting a copy or using a hash set gives a correct answer but doesn't meet the full challenge.
Examples
Input: nums = [1, 3, 4, 2, 2]
Output: 2
Input: nums = [3, 1, 3, 4, 2]
Output: 3
Input: nums = [1, 1]
Output: 1
Constraints
1 <= n <= 10^5
nums.length == n + 1
1 <= nums[i] <= n
Exactly one value in nums repeats (one or more extra times).
Approach
The simplest approach ignores the space limit: store each value in a set as you scan, and the first value seen twice is the answer.
A better approach binary searches the range of possible values. For a midpoint m, count the entries <= m. If the count exceeds m, the duplicate is in the lower half; otherwise it's in the upper half.
The approach that meets every constraint treats the array as a linked list: position i is a node whose "next" is nums[i]. Two positions always point at the same value, so there's always a cycle, and its entry point is the duplicate. Floyd's tortoise-and-hare finds it with no extra memory and without changing the array.
Solutions
Brute Force — Track Seen Values
Scan the array, storing each value in a set. The first value seen twice is the duplicate. Simple and fast, but it uses memory proportional to the array's size, which the strict version doesn't allow.
function findDuplicate(nums) {
const seen = new Set();
for (const num of nums) {
if (seen.has(num)) return num;
seen.add(num);
}
return -1; // unreachable given the problem's guarantees
}Time: O(n) · Space: O(n)
Binary Search on the Value Range
Search the values 1 to n instead of array positions. For a midpoint m, count the numbers <= m. If there are more than m, there are "too many" small values, so the duplicate is between 1 and m; otherwise it's above m. Halving the range repeatedly finds the duplicate.
function findDuplicate(nums) {
let low = 1;
let high = nums.length - 1;
while (low < high) {
const mid = Math.floor((low + high) / 2);
let countLessOrEqual = 0;
for (const num of nums) {
if (num <= mid) countLessOrEqual++;
}
if (countLessOrEqual > mid) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}Time: O(n log n) · Space: O(1)
Optimal — Cycle Detection (Floyd's Tortoise and Hare)
Treat the array as a linked list where the node after position i is position nums[i]. There are n + 1 positions but only n values, so at least two positions point at the same value. That makes the list loop back on itself, and the value where the cycle starts is the duplicate.
First, run slow/fast pointers (one step and two steps) from index 0 until they meet inside the cycle. Then reset one pointer to the start and move both one step at a time. Where they meet again is the cycle's entry point — the duplicate.
function findDuplicate(nums) {
// Phase 1: find a meeting point inside the cycle.
let slow = nums[0];
let fast = nums[0];
do {
slow = nums[slow];
fast = nums[nums[fast]];
} while (slow !== fast);
// Phase 2: find the entrance to the cycle, which is the duplicate.
slow = nums[0];
while (slow !== fast) {
slow = nums[slow];
fast = nums[fast];
}
return slow;
}Time: O(n) · Space: O(1)