dsa · medium
Find All Duplicates in an Array
nums has length n. Every value is an integer in 1..n, and each value appears **once or twice**. Return every value that appears twice, in any order. If nothing is repeated, return an empty list.
Arguments
nums— length-n array of values in 1..n, each appearing once or twice
Example
nums = [4,3,2,7,8,2,3,1]: 2 and 3 each appear twice → [2,3] (any order).
nums = [1,1,2] → [1].
nums = [1] → [].
Constraints
1 <= nums.length <= 5 * 10^4 1 <= nums[i] <= nums.length each value appears once or twice Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.
Examples
Example 1
Input: [4, 3, 2, 7, 8, 2, 3, 1] Expected: [3,2]
Example 2
Input: [1, 1, 2] Expected: [1]
Example 3
Input: [1] Expected: []