Find the Duplicate Number

Given an array of n+1 integers where each integer is between 1 and n inclusive, return the duplicate number. Index placement or cycle detection both exploit the constrained value range.

Input Format

nums = array containing one repeated value

Output Format

the duplicate number

Constraints

  • 1 <= nums.length <= 10^5; 1 <= nums[i] <= nums.length - 1

Examples

Example 1:

Input:

nums = [1,3,4,2,2]

Output:

2

Explanation:

The number 2 appears twice.

Example 2:

Input:

nums = [3,1,3,4,2]

Output:

3

Explanation:

The duplicate number is 3.

Loading...
Find the Duplicate Number