0287. Find the Duplicate Number
Description
**Input:** nums = [1,3,4,2,2]
**Output:** 2**Input:** nums = [3,1,3,4,2]
**Output:** 3**Input:** nums = [1,1]
**Output:** 1**Input:** nums = [1,1,2]
**Output:** 1ac
Last updated
**Input:** nums = [1,3,4,2,2]
**Output:** 2**Input:** nums = [3,1,3,4,2]
**Output:** 3**Input:** nums = [1,1]
**Output:** 1**Input:** nums = [1,1,2]
**Output:** 1Last updated
class Solution {
public int findDuplicate(int[] nums) {
// fast/slow pointers
int fast = nums[0], slow = nums[0];
do {
slow = nums[slow];
fast = nums[nums[fast]];
}while (fast != slow);
int i = nums[0];
while (i != slow) {
i = nums[i];
slow = nums[slow];
}
return i;
}
}