- Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy path_1481.java
32 lines (29 loc) · 1.1 KB
/
_1481.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
packagecom.fishercoder.solutions.secondthousand;
importjava.util.HashMap;
importjava.util.LinkedHashMap;
importjava.util.Map;
publicclass_1481 {
publicstaticclassSolution1 {
publicintfindLeastNumOfUniqueInts(int[] arr, intk) {
Map<Integer, Integer> unSortedMap = newHashMap<>();
for (intnum : arr) {
unSortedMap.put(num, unSortedMap.getOrDefault(num, 0) + 1);
}
// LinkedHashMap preserve the ordering of elements in which they are inserted
LinkedHashMap<Integer, Integer> sortedMap = newLinkedHashMap<>();
unSortedMap.entrySet().stream()
.sorted(Map.Entry.comparingByValue())
.forEachOrdered(x -> sortedMap.put(x.getKey(), x.getValue()));
intleastUniq = 0;
for (intkey : sortedMap.keySet()) {
intcount = sortedMap.get(key);
if (k >= count) {
k -= count;
} else {
leastUniq++;
}
}
returnleastUniq;
}
}
}