The One Repeated Number
Problem statement
Numbers arrive one after another, and you are given them as the array nums in arrival order. Let n be the number of values.
The values are every whole number from 1 to n - 1, each exactly once, except that one of them arrives a second time. The two copies can be anywhere. Return the value that arrives twice.
Function
findRepeated(nums: int[]) → intExamples
Example 1
nums = [5,1,4,2,5,3]return = 5There are 6 numbers, so the values are 1 to 5 with one of them twice. 5 appears at index 0 and again at index 4.
Example 2
nums = [2,4,1,3,4,5]return = 4There are 6 numbers, so the values are 1 to 5. 4 appears at index 1 and again at index 4; every other value appears once.
Example 3
nums = [6,3,1,5,2,4,3]return = 3The values 1 to 6 each appear once, except 3, which appears at index 1 and at index 6.
Constraints
2 <= n <= 10^4, wheren = nums.length1 <= nums[i] <= n - 1- Every value from
1ton - 1appears innums, and exactly one of them appears twice.