You are given a list of integers nums. It has n + 1 elements, and each element is between 1 and n, inclusive.
Exactly one value in nums is repeated: it appears two or more times. Every other value in nums appears at most once.
Return the repeated value.
Note that the repeated value can appear more than twice, as in Example 3.
Examples
Example 1
Input: nums = [1,3,4,2,2]
Output: 2
Explanation: There are 5 elements, so n = 4. The values 1, 3, and 4 each appear once, and 2 appears twice. The answer is 2.
Example 2
Input: nums = [3,1,3,4,2]
Output: 3
Explanation: n = 4. The values 1, 2, and 4 each appear once, and 3 appears twice, at index 0 and index 2. The answer is 3.
Example 3
Input: nums = [3,3,3,3,3]
Output: 3
Explanation: n = 4. The only value present is 3, and it appears five times. The answer is 3.
Constraints
1≤n≤105
nums.length=n+1
1≤nums[i]≤n
Exactly one value in nums appears two or more times. Every other value in nums appears at most once.
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up