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 b9037b5 commit f58ae30Copy full SHA for f58ae30
top-k-frequent-elements/Tessa1217.java
@@ -0,0 +1,34 @@
1
+
2
+/**
3
+ * 정수 배열 nums와 정수 k가 주어질 때 자주 반복되는 값을 K개 반환
4
+ * */
5
+import java.util.Map;
6
+import java.util.HashMap;
7
+import java.util.Arrays;
8
9
+public class Solution {
10
11
+ public int[] topKFrequent(int[] nums, int k) {
12
+ Map<Integer, Integer> map = new HashMap<>();
13
+ for (int i = 0; i < nums.length; i++) {
14
+ map.put(nums[i], map.getOrDefault(nums[i], 0) + 1);
15
+ }
16
17
+ int[][] arr = map.entrySet().stream()
18
+ .map((e) -> new int[]{e.getKey(), e.getValue()})
19
+ .toArray(int[][]::new);
20
21
+ Arrays.sort(arr, (f1, f2) -> f2[1] - f1[1]);
22
23
+ int[] frequency = new int[k];
24
+ for (int i = 0; i < k; i++) {
25
+ frequency[i] = arr[i][0];
26
27
28
+ return frequency;
29
30
31
32
+}
33
34
0 commit comments