Reduce Array Size to The Half (LeetCode 1338): Solution and Explanation

Medium
Array
Hash Table
Greedy
Sorting
Heap Priority Queue

Pick as few distinct values as possible so that deleting every copy of them removes at least half of the array, and return how many values you picked.

Intuition

Choosing a value deletes all of its copies, and every choice adds exactly 1 to the answer. So each choice should delete as many elements as possible.

That points straight at the most frequent values. If an optimal set used a value with fewer copies while a more frequent value was left out, swapping them would delete at least as much at the same cost.

So count, sort the counts from largest to smallest, and keep adding until the removed total reaches half the length.

Approach: Greedy on sorted frequencies

  1. Count how many times each value appears.
  2. Sort the counts in decreasing order.
  3. Walk the counts, adding each to removed and increasing chosen.
  4. Stop as soon as removed * 2 >= len(arr) and return chosen.

Step-by-step example

arr = [3,3,3,3,5,5,5,2,2,7]
  1. Counts: 3 appears 4 times, 5 three times, 2 twice, 7 once.
  2. Sorted counts: [4, 3, 2, 1]. Half of 10 is 5.
  3. Pick 3: removed = 4, still below 5.
  4. Pick 5: removed = 7, which reaches 5. Stop.
  5. Return 2.

Code: Python, Java, C++

1class Solution:
2    def minSetSize(self, arr: List[int]) -> int:
3        counts = sorted(Counter(arr).values(), reverse=True)
4        removed = 0
5        chosen = 0
6        for c in counts:
7            removed += c
8            chosen += 1
9            if removed * 2 >= len(arr):
10                break
11        return chosen
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 sort by frequency O(n)
Greedy on sorted frequencies O(n log n)
Max-heap of counts O(n + k log m)
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 sorted list of counts hold up to n entries.

Other approaches

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

No count exceeds n, so bucket the counts by value and walk the buckets from n down to 1, taking counts until half is removed.

Max-heap of counts (time O(n + k log m), space O(n))

Push all counts into a max-heap and pop the largest until half the array is removed. Same idea, with O(k log m) work for k pops.

Common mistakes

  • Sorting the values instead of their counts. The size of a value does not matter, only how often it appears.
  • Using a strict comparison. Removing exactly half is enough, so the check is removed * 2 >= n.
  • Counting the elements left instead of removed and stopping one step too late or too early.
  • Picking the least frequent values first, which is the right greedy for a different problem (Least Number of Unique Integers after K Removals).

Frequently asked questions

Why is taking the most frequent values optimal?

Every value in the set costs one. For a fixed set size, the largest counts remove the most elements, so if the top k counts cannot reach half, no k values can.

Can the answer be larger than n / 2?

No. Even when all values are distinct, n / 2 values remove exactly half of the array.

Does it matter which of several equal counts I pick?

No. Equal counts remove the same number of elements, so the answer is the same.