Skip to content

Commit 32ed2ba

Browse files
committed
top k frequent
1 parent 58e2901 commit 32ed2ba

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
class Solution {
5+
public static int[] topKFrequent(int[] nums, int k) {
6+
Map<Integer, Integer> myMap = new HashMap<>();
7+
for (int num : nums) {
8+
myMap.put(num, myMap.getOrDefault(num, 0) + 1);
9+
}
10+
return myMap.entrySet()
11+
.stream()
12+
.sorted((v1, v2) -> Integer.compare(v2.getValue(),v1.getValue()))
13+
.map(Map.Entry::getKey)
14+
.mapToInt(Integer::intValue)
15+
.toArray();
16+
}
17+
}

0 commit comments

Comments
 (0)