Find the Duplicate Number
MediumLinked List
Decode it first. Clues, constraints, pattern and plan before any code.You get an array nums of n + 1 integers, where every value is between 1 and n. By the pigeonhole principle at least one value repeats, and in fact exactly one value does (it may appear more than twice). Return that value without modifying the array and using only constant extra space.
nums = [1, 3, 4, 2, 2] → 2
nums = [3, 1, 3, 4, 2] → 3
nums = [3, 3, 3, 3, 3] → 3Hint for the shape: treat each index i as a node whose next is nums[i].
Constraints
1 <= n <= 10^5nums.length == n + 11 <= nums[i] <= n- Exactly one value appears two or more times.