Find the Repeated Numbers
You are given an integer array nums of length n.
Each element in the array is in the range [1, n], and every number appears either once or twice.
Your task is to return all numbers that appear exactly twice.
The returned duplicate numbers should be listed in the same order as their second occurrence appears in the array.
You must solve the problem in:
- O(n) time
- constant auxiliary space
Note: The space used for the output array does not count as extra space.
Example 1:
Input: nums = [4,3,2,7,8,2,3,1]
Output: [2,3]
Explanation:
- 2 appears twice
- 3 appears twice
So, the answer is [2,3].
Example 2:
Input: nums = [5,4,6,5,1,2,2,8]
Output: [5,2]
Explanation:
- 5 appears twice
- 2 appears twice
So, the answer is [5,2].
Now Your Turn!
Pick the correct output for the given inputInput: nums = [1,2,2,4,5,6,7,8]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- n == nums.length
- 1 <= n <= 105
- 1 <= nums[i] <= n
- Each value in nums appears once or twice only