Least Number of Unique Integers after K Removals (LeetCode 1481): Solution and Explanation

Medium
Array
Hash Table
Greedy
Sorting
Counting

Remove exactly k elements from arr so that as few distinct values as possible are left, and return that number of distinct values.

Intuition

A value only stops counting when every one of its copies has been removed. Removing some but not all copies does nothing for the answer.

So think in groups: each distinct value is a pile, and erasing a pile costs as many removals as it has copies. With a fixed budget k, the way to erase the most piles is to erase the smallest ones.

Sort the pile sizes ascending and pay for them one by one until the next pile costs more than what is left. Any leftover removals go into a surviving pile and do not change the count.

Approach: Count, sort counts, spend k on the smallest

  1. Count how many times each value appears.
  2. Sort the counts in increasing order and set remaining to the number of counts.
  3. For each count c: if k < c, stop. Otherwise subtract c from k and decrease remaining.
  4. Return remaining.

Step-by-step example

arr = [5,5,4], k = 1
  1. Counts: 5 appears 2 times, 4 appears once.
  2. Sorted counts: [1, 2]. remaining = 2.
  3. Count 1 (value 4): k = 1 is enough. Remove it: k = 0, remaining = 1.
  4. Count 2 (value 5): k = 0 is less than 2, so stop.
  5. Return 1.

Code: Python, Java, C++

1class Solution:
2    def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int:
3        freq = Counter(arr)
4        counts = sorted(freq.values())
5        remaining = len(counts)
6        for c in counts:
7            if k < c:
8                break
9            k -= c
10            remaining -= 1
11        return remaining
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Bucket by frequency O(n)
Count, sort counts, spend k on the smallest O(n log n)
Min-heap of counts O(n log n)
Time

O(n log n)

Counting is O(n). Sorting up to n distinct counts is O(n log n) in the worst case.

Space

O(n)

The frequency map and the list of counts can each hold up to n entries.

Other approaches

Bucket by frequency (time O(n), space O(n))

No count exceeds n, so place counts in buckets indexed by frequency and walk the buckets from 1 upward, removing as many whole groups from each bucket as k allows.

Min-heap of counts (time O(n log n), space O(n))

Push every count into a min-heap and pop while the smallest count fits in k. Same greedy order, slightly more overhead than sorting.

Common mistakes

  • Removing the most frequent values first. That spends the budget on big piles and erases fewer values.
  • Counting a partly removed group as gone. Only groups whose count fits completely in k disappear.
  • Continuing the loop after a group does not fit. Every later count is at least as large, so it will not fit either.
  • Forgetting k = 0, which must return the number of distinct values unchanged.

Frequently asked questions

Why is removing the smallest groups first optimal?

If an optimal plan erases some group while a smaller group survives, swapping them costs no more removals and erases the same number of groups. Repeating this gives the smallest-first plan.

What happens to leftover removals?

They are taken from any surviving group. That group keeps at least one copy, because its count was larger than the leftover k, so the number of distinct values does not change.

Do we need the actual values or only the counts?

Only the counts. The values are needed just to group equal elements together.