Find every value that appears more than once in an integer array, sorted ascending

Quick Overview

Given an array of up to 100,000 integer identifiers, return every value that appears more than once, listed once each in ascending order. The problem tests frequency tracking with hash sets, deduplicating the output, and the cost of producing a sorted result.

Find every value that appears more than once in an integer array, sorted ascending

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of integers `nums` representing identifiers from a large dataset, return a list of all values that appear more than once. Each duplicated value should appear exactly once in the result, and the result must be sorted in ascending order. If no duplicates exist, return an empty list. ### Function Signature ```python def find_duplicates(nums: list[int]) -> list[int]: ``` ### Rules - A value counts as duplicated when it occurs two or more times in `nums`. A value that occurs three or more times is still listed once. - Sort the result in ascending numeric order, so negative values come before positive ones. - Return `[]` when every value is unique. ### Constraints - `1 <= len(nums) <= 10^5` - `-10^9 <= nums[i] <= 10^9` ### Examples ```text Input: nums = [1, 2, 3, 2, 4, 5, 3, 6] Output: [2, 3] ``` 2 appears twice and 3 appears twice. Every other value appears once, and the sorted result is `[2, 3]`. ```text Input: nums = [1, 2, 3, 4, 5] Output: [] ``` All elements are unique, so the result is an empty list. ```text Input: nums = [5, -1, 5, 5, -1, 0] Output: [-1, 5] ``` 5 appears three times and -1 appears twice, so each is listed once. 0 appears once and is left out, and -1 sorts before 5.

Overview: Given an array of up to 100,000 integer identifiers, return every value that appears more than once, listed once each in ascending order. The problem tests frequency tracking with hash sets, deduplicating the output, and the cost of producing a sorted result.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers nums representing identifiers from a large dataset, return a list of all values that appear more than once.

Each duplicated value should appear exactly once in the result, and the result must be sorted in ascending order. If no duplicates exist, return an empty list.

Function Signature

def find_duplicates(nums: list[int]) -> list[int]:

Rules

  • A value counts as duplicated when it occurs two or more times in nums . A value that occurs three or more times is still listed once.
  • Sort the result in ascending numeric order, so negative values come before positive ones.
  • Return [] when every value is unique.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9

Examples

Input: nums = [1, 2, 3, 2, 4, 5, 3, 6]
Output: [2, 3]

2 appears twice and 3 appears twice. Every other value appears once, and the sorted result is [2, 3].

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

All elements are unique, so the result is an empty list.

Input: nums = [5, -1, 5, 5, -1, 0]
Output: [-1, 5]

5 appears three times and -1 appears twice, so each is listed once. 0 appears once and is left out, and -1 sorts before 5.