Find All Duplicates in an Array

Medium

Description

Given an integer array nums of length n where all the integers of nums are in the range [1, n] and each integer appears once or twice, return an array of all the integers that appear twice.

Examples

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

2 and 3 appear twice.

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

Only the number 1 appears twice in the array. The number 2 appears once, so it's not included in the result.

Input:nums = [5,4,6,7,9,3,10,9,5,6]
Output:[5,6,9]
Explanation:

The numbers 5, 6, and 9 each appear exactly twice in the array. All other numbers (4, 7, 3, 10) appear only once.

Constraints

  • n == nums.length
  • 1 ≤ n ≤ 10^5
  • 1 ≤ nums[i] ≤ n

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!