#442

Find All Duplicates in an Array

pupil · 470 · lc medium +26 · verified · 76.8% accepted · 11,113 likes · top 88%

play →

Description

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: []

Code

1
2
3