Reduce Array Size to The Half (LeetCode 1338): Solution and Explanation
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
- Count how many times each value appears.
- Sort the counts in decreasing order.
- Walk the counts, adding each to
removedand increasingchosen. - Stop as soon as
removed * 2 >= len(arr)and returnchosen.
Step-by-step example
arr = [3,3,3,3,5,5,5,2,2,7]
- Counts: 3 appears 4 times, 5 three times, 2 twice, 7 once.
- Sorted counts: [4, 3, 2, 1]. Half of 10 is 5.
- Pick 3: removed = 4, still below 5.
- Pick 5: removed = 7, which reaches 5. Stop.
- 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 chosen1class Solution {
2 public int minSetSize(int[] arr) {
3 Map<Integer, Integer> freq = new HashMap<>();
4 for (int x : arr) freq.merge(x, 1, Integer::sum);
5 List<Integer> counts = new ArrayList<>(freq.values());
6 counts.sort(Collections.reverseOrder());
7 int removed = 0, chosen = 0;
8 for (int c : counts) {
9 removed += c;
10 chosen++;
11 if (removed * 2 >= arr.length) {
12 break;
13 }
14 }
15 return chosen;
16 }
17}1class Solution {
2public:
3 int minSetSize(vector<int>& arr) {
4 unordered_map<int, int> freq;
5 for (int x : arr) freq[x]++;
6 vector<int> counts;
7 for (auto& [v, c] : freq) counts.push_back(c);
8 sort(counts.rbegin(), counts.rend());
9 int removed = 0, chosen = 0;
10 for (int c : counts) {
11 removed += c;
12 chosen++;
13 if (removed * 2 >= (int)arr.size()) {
14 break;
15 }
16 }
17 return chosen;
18 }
19};Time and space complexity
O(n log n)
Counting is O(n). Sorting up to n distinct counts is O(n log n) in the worst case.
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.