Problem Description
Given an integer array, choose a set of integers to remove (i.e., remove all occurrences of each chosen integer) such that at least half of the integers in the original array are removed. Return the minimum size of the set.
Key Insights
- Count the frequency of each integer in the array.
- Removing numbers with higher frequencies first minimizes the number of distinct integers needed for removal.
- Sort the frequencies in descending order and iterate until the cumulative count meets or exceeds half of the original array's size.
- Use a greedy strategy to decide which numbers to remove.
Space and Time Complexity
Time Complexity: O(n log n) — O(n) to count frequencies and O(n log n) to sort them. Space Complexity: O(n) — for storing the frequency count.
Solution
We use a hash table to count occurrences of every number in the array. After obtaining the frequency of each number, we sort these counts in descending order. Then, we iterate over the sorted frequencies, adding each frequency to a cumulative sum until we reach at least half of the original array length. The number of iterations required gives us the minimum set size. This greedy approach ensures that we are always removing the most frequent elements first to efficiently reduce the array size.