Find All Duplicates in an Array

Given an array containing values from 1 to n, return all values that appear twice. Cyclic placement shows duplicates as values that cannot be placed in their home positions.

Input Format

nums = array of integers from 1 to n

Output Format

all duplicated values

Constraints

  • 1 <= nums.length <= 10^5; 1 <= nums[i] <= nums.length

Examples

Example 1:

Input:

nums = [4,3,2,7,8,2,3,1]

Output:

[2,3]

Explanation:

2 and 3 appear twice.

Example 2:

Input:

nums = [1,1,2]

Output:

[1]

Explanation:

1 is the only duplicate.

Loading...
Find All Duplicates in an Array