Skip to main content
Back to problems
Leetcode
Medium
Arrays
Hash Maps
Sorting
Heaps
Amazon
Least Number Of Unique Integers After K Removals

Remove exactly kk elements from an array so that the number of distinct integers left is as small as possible.

Acceptance 63%
Problem Statement

Problem

You are given an integer array arr and an integer k.

In one move, you may remove any single element from the array. After performing exactly k removals, return the minimum possible number of distinct integers remaining in the array.

The goal is not to minimize the final array length; it is to eliminate as many different values as possible by removing all occurrences of low-frequency values first.

Key idea

A value disappears only when all of its occurrences are removed. Use the removal budget on values with the smallest frequencies first.

Input Format

  • An integer array arr
  • An integer k

You may assume the input follows the usual interview-style constraints for this problem.

Output Format

  • Return an integer: the minimum number of unique integers remaining after exactly k removals.

Constraints

  • 1 <= arr.length
  • 0 <= k <= arr.length
  • Elements of arr can be any integers

Exact platform constraints are not provided here; use standard interview assumptions.

Examples
Sample cases returned by the problem API.

Example 1

Input

arr = [5,5,4], k = 1

Output

1

Explanation

Remove one 5 to get [5,4]. The distinct integers are {5,4}, so the answer is 2? Wait, better choice is to remove 4, leaving [5,5] with only 1 distinct integer. Thus the minimum is 1.

Example 2

Input

arr = [4,3,1,1,3,3,2], k = 3

Output

2

Explanation

Frequencies are: 1 -> 2, 2 -> 1, 3 -> 3, 4 -> 1. Remove all of 2 and 4 using 2 deletions, then remove one 1 or one 3 with the last deletion. The minimum number of distinct integers left is 2.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.