Skip to content
Onsite

Find the Duplicate Number

MediumP2Coding

nums has n + 1 integers, each in the range 1..n. Exactly one value is repeated (it may appear two or more times); every other value appears once. Return the repeated value.

Do not modify nums and use only O(1) extra space.

Example 1

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

Example 2

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

Example 3

Input
nums = [3,3,3,3,3]
Output
3

Constraints

  • 2 <= nums.length <= 10^5
  • 1 <= nums[i] <= nums.length - 1
  • Exactly one value is repeated.

Hints

Run checks the 3 visible tests, Submit all 10.