We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 58e2901 commit 32ed2baCopy full SHA for 32ed2ba
top-k-frequent-elements/eunhwa99.java
@@ -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