Find Duplicates

Medium~15 min

Given an array of integers nums, return an array of all elements that appear more than once. The result can be in any order, and each duplicate value should only appear once in the output.

For example, given [4, 3, 2, 7, 8, 2, 3, 1], the duplicates are [2, 3] (in any order).

Examples

Example 1
Input: nums = [4, 3, 2, 7, 8, 2, 3, 1]
Output: [2, 3]
Explanation: 2 and 3 each appear twice. Return them in any order.
Example 2
Input: nums = [1, 1, 1]
Output: [1]
Explanation: 1 appears three times, but we only include it once in the result.
Example 3
Input: nums = [1, 2, 3]
Output: []
Explanation: No element appears more than once.

Constraints

  • 0 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • Output should contain each duplicate value exactly once.
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run