# [LeetCode 1338. Reduce Array Size to The Half](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge-2021/608/week-1-july-1st-july-7th/3804/) ### Daily challenge Jul 6, 2021 (MEDIAN) >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. :::info **Example 1:** **Input:** arr = [3,3,3,3,5,5,5,2,2,7] **Output:** 2 **Explanation:** Choosing {3,7} will make the new array [5,5,5,2,2] which has size 5 (i.e equal to half of the size of the old array). Possible sets of size 2 are {3,5},{3,2},{5,2}. Choosing set {2,7} is not possible as it will make the new array [3,3,3,3,5,5,5] which has size greater than half of the size of the old array. ::: :::info **Example 2:** **Input:** arr = [7,7,7,7,7,7] **Output:** 1 **Explanation:** The only possible set you can choose is {7}. This will make the new array empty. ::: :::info **Example 3:** **Input:** arr = [1,9] **Output:** 1 ::: :::info **Example 4:** **Input:** arr = [1000,1000,3,7] **Output:** 1 ::: :::info **Example 5:** **Input:** arr = [1,2,3,4,5,6,7,8,9,10] **Output:** 5 ::: :::warning **Constraints:** * 1 <= arr.length <= 105 * arr.length is even. * 1 <= arr[i] <= 105 ::: --- ### Approach 1 : HashMap & Sort :bulb: **`156 ms ( 83% )`** **`O(nlogn)`** >1. Using **HashMap** to count frequency of each number. >2. Get the array of frequency and **sort** it. >3. Removing the elements from the largest frequency until we have removed at least half of **`arr`**. ```cpp=1 class Solution { public: int minSetSize(vector<int>& arr) { int size = arr.size(); unordered_map<int, int > freq; for(int i=0; i<size; i++){ freq[arr[i]]++; } vector<int > list; for(auto it : freq){ list.push_back(it.second); } sort(list.begin(), list.end()); int ans = 0; int count = 0; for(int i=list.size()-1; i>=0; i--){ ans++; count += list[i]; if(count >= size / 2) return ans; } return size; } }; ```