image_2022-08-19_01-28-07.png
49.7 KB
#medium
#N1338. Reduce Array Size to The Half
problem link
#solution
#N1338. Reduce Array Size to The Half
problem link
#solution
class Solution {
public int minSetSize(int[] arr) {
int count=0, sum=0;
Map<Integer, Integer> map = new HashMap<>();
for(int n: arr){
map.put(n, map.getOrDefault(n, 0) +1);
}
List<Map.Entry<Integer, Integer>> list = new LinkedList<>(map.entrySet());
Collections.sort(list, (i1, i2) -> i2.getValue().compareTo(i1.getValue()));
for (Map.Entry<Integer, Integer> aa : list) {
sum+=aa.getValue();
count++;
if(sum>=arr.length/2){
return count;
}
}
return -1;
}
}