Find the Duplicate Number
Problem statement
Find the Duplicate Number
You are given an integer array nums of length n. It contains every integer from 1 through n - 1 exactly once, except for one value that appears twice.
Return the duplicated value.
Function
findDuplicateNumber(nums: int[]) → intExamples
Example 1
nums = [1,3,4,2,2]return = 2The value 2 is the only number that appears twice.
Example 2
nums = [3,1,3,4,2]return = 3The value 3 appears at two positions.
Example 3
nums = [1,1]return = 1With n = 2, the only valid value is 1, and it appears twice.
Constraints
2 ≤ nums.length ≤ 100,000- Every value is between
1andnums.length - 1, inclusive. - Exactly one distinct value appears twice, and every other value appears once.