Problem
You are given an integer array arr
. You can choose a set of integers and remove all the occurrences of these integers in the array.
Return the minimum size of the set so that at least half of the integers of the array are removed.
Examples
Example 1:
|
|
Example 2:
|
|
Solution
Method 1 - Create Array on Frequency Map of Array Elements
Approach
- Count the frequency of each element in the array using a hash map.
- Store all frequencies in an array.
- Sort the frequencies in ascending order.
- Starting from the largest frequency, keep removing elements (i.e., add frequencies to a running total) until at least half of the array is removed.
- Return the minimum number of elements needed to reach at least half the array size.
Code
|
|
Complexity
- ⏰ Time complexity:
O(n log n)
- O(n) to build the frequency map
- O(n) to build the frequencies array
- O(n log n) to sort the frequencies
- O(n) to accumulate removals
- 🧺 Space complexity:
O(n)
- For the frequency map and the frequencies array
Method 2 - Frequency Map and Bucket Sort 🏆
Approach
- Count the frequency of each element in the array using a hash map.
- Use a counting array (bucket) to count how many numbers have each possible frequency.
- Start from the highest frequency and keep removing elements (adding frequencies to a running total) until at least half of the array is removed.
- Return the minimum number of elements needed to reach at least half the array size.
Code
|
|
Complexity
- ⏰ Time complexity:
O(n)
- O(n) to build the frequency map
- O(n) to build the counting array
- O(n) to accumulate removals (since the sum of all frequencies is n)
- 🧺 Space complexity:
O(n)
- For the frequency map and the counting array
Method 3 - Use Max Heap on Frequency Map of Array Elements
Approach
- Count the frequency of each element in the array using a hash map.
- Add all frequencies to a max heap (priority queue).
- Repeatedly remove the largest frequency from the heap, adding it to a running total, until at least half of the array is removed.
- Return the minimum number of elements needed to reach at least half the array size.
Code
|
|
Complexity
- ⏰ Time complexity:
O(n log n)
- O(n) to build the frequency map
- O(n) to add all frequencies to the max heap
- Each heap operation is O(log n), and in the worst case, we may remove up to n/2 elements, so total heap operations are O(n log n).
- 🧺 Space complexity:
O(n)
- For the frequency map and the max heap