Problem · Array
Missing and Repeated Number
Learn this problemProblem statement
An unsorted integer array nums has length n and contains values from 1 through n. Exactly one value appears twice, exactly one value is missing, and every other value appears once.
Return an array containing the repeated value followed by the missing value. Solve the task in O(n) time and O(1) extra space without changing nums.
Function
findRepeatedAndMissing(nums: int[]) → int[]Examples
Example 1
nums = [3,1,2,5,3]return = [3,4]The value 3 occurs twice and 4 is absent.
Example 2
nums = [1,1]return = [1,2]The repeated value is 1 and the missing value is 2.
Example 3
nums = [2,3,2,4]return = [2,1]The missing value can be smaller than the repeated value.
Constraints
2 <= n <= 200000.1 <= nums[i] <= n.- Exactly one value occurs twice and exactly one value is absent.