Find the Duplicate Number Without Modifying the Array
Problem statement
An array of length n+1 contains values from 1 through n. Exactly one value occurs more than once.
Return that duplicate without modifying the array and using constant extra space.
Function
findDuplicate(nums: int[]) → intExamples
Example 1
nums = [1,3,4,2,2]return = 2Two is the repeated value.
Example 2
nums = [3,1,3,4,2]return = 3Three is repeated.
Example 3
nums = [1,1]return = 1The smallest valid array repeats one.
Constraints
1 <= n <= 100000.- Exactly one distinct value is duplicated, possibly more than twice.