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 fe794af commit dd0a5aaCopy full SHA for dd0a5aa
top-k-frequent-elements/sora0319.java
@@ -1,5 +1,29 @@
1
import java.util.*;
2
+// 다른 방안
3
+class Solution {
4
+ public int[] topKFrequent(int[] nums, int k) {
5
+ Map <Integer,Integer> counting = new HashMap<>();
6
+
7
+ for(int n : nums){
8
+ if(!counting.containsKey(n)){
9
+ counting.put(n,0);
10
+ }
11
+ counting.put(n, counting.get(n)+1);
12
13
14
+ List<Map.Entry<Integer,Integer>>countList = new LinkedList<>(counting.entrySet());
15
+ countList.sort(Map.Entry.comparingByValue(Comparator.reverseOrder()));
16
17
+ int[] answer = countList.stream()
18
+ .limit(k)
19
+ .mapToInt(Map.Entry::getKey)
20
+ .toArray();
21
+ return answer;
22
23
+}
24
25
26
+// 초안
27
class Solution {
28
public int[] topKFrequent(int[] nums, int k) {
29
Map<Integer,Integer> counts = new HashMap<>();
0 commit comments