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 4bf599c commit 09eed96Copy full SHA for 09eed96
top-k-frequent-elements/sj.java
@@ -0,0 +1,17 @@
1
+import java.util.*;
2
+class Solution {
3
+ public int[] topKFrequent(int[] nums, int k) {
4
+ Map<Integer,Integer> count = new HashMap<>();
5
+ for(int i=0;i<nums.length;i++){
6
+ count.put(nums[i],count.getOrDefault(nums[i],0)+1);
7
+ }
8
+ List<Integer> sortedCount = new ArrayList<>(count.keySet());
9
+ sortedCount.sort((a,b)->count.get(b)-count.get(a));//value 기준 키 정렬
10
+ int[] answer = new int[k];
11
+ for(int i=0;i<k;i++){
12
+ answer[i] = sortedCount.get(i);
13
14
+
15
+ return answer;
16
17
+}
0 commit comments