1
class Solution {
2
public int minSetSize(int[] arr) {
3
int size = arr.length;
4
int deletedSize = 0;
5
int countIteration = 0;
6
Map<Integer, Integer> hashMap = new HashMap<>();
7
Queue<Map.Entry<Integer, Integer>> queue =
8
new PriorityQueue<>((a, b) -> b.getValue() - a.getValue());
9

10
for (int i = 0; i < size; i++) {
11
if (hashMap.get(arr[i]) != null) hashMap.put(arr[i], hashMap.get(arr[i]) + 1);
12
else hashMap.put(arr[i], 1);
13
}
14

15
for (Map.Entry<Integer, Integer> entry : hashMap.entrySet()) {
16
queue.add(entry);
17
}
18

19
while (!queue.isEmpty()) {
20
int totalOccurence = queue.poll().getValue();
21
deletedSize += totalOccurence;
22
countIteration++;
23
if (deletedSize >= size / 2) return countIteration;
24
}
25
return countIteration;
26
}
27
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0