Skip to content

Commit ce9b140

Browse files
committed
WEEK 1 Solutions
1 parent ab76aec commit ce9b140

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)