Medium

Quiz

#442 Find All Duplicates in an Array

APPROACH

An integer array nums of length n has every element in the range [1, n], and each element appears exactly once or twice. Find all elements that occur exactly twice and return them in any order.

Constraints: O(n) time and O(1) extra space (not counting the output array).

Example 1:

Input: nums = [4,3,2,7,8,2,3,1]
Output: [2,3]

Example 2:

Input: nums = [1,1,2]
Output: [1]

Example 3:

Input: nums = [1]
Output: []
1 of 4
1:00

What is the optimal approach for this problem?