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 ab76aec commit ce9b140Copy full SHA for ce9b140
1 file changed
top-k-frequent-elements/juhui-jeong.java
@@ -0,0 +1,24 @@
1
+class Solution {
2
+ public int[] topKFrequent(int[] nums, int k) {
3
+ Map<Integer, Integer> map = new HashMap<>();
4
+
5
+ for (int i = 0; i< nums.length; i++) {
6
+ if (map.containsKey(nums[i])) {
7
+ map.put(nums[i], map.get(nums[i])+1);
8
+ } else {
9
+ map.put(nums[i], 1);
10
+ }
11
12
13
+ List<Map.Entry<Integer, Integer>> entryList = new LinkedList<>(map.entrySet());
14
+ entryList.sort(Map.Entry.comparingByValue(Collections.reverseOrder()));
15
16
+ int[] result = new int[k];
17
18
+ for(int i =0; i < k; i++) {
19
+ result[i] = entryList.get(i).getKey();
20
21
22
+ return result;
23
24
+}
0 commit comments