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 f9b368d commit 00d02f2Copy full SHA for 00d02f2
top-k-frequent-elements/choidabom.ts
@@ -0,0 +1,19 @@
1
+// https://leetcode.com/problems/top-k-frequent-elements/
2
+
3
+// TC: O(nlogn)
4
+// SC: O(n)
5
6
+function topKFrequent(nums: number[], k: number): number[] {
7
+ const map = new Map()
8
9
+ for (const num of nums) {
10
+ if (map.has(num)){
11
+ map.set(num, map.get(num) + 1)
12
+ } else {
13
+ map.set(num, 1)
14
+ }
15
16
17
+ const sortedMap = [...map].sort((a, b)=> b[1]- a[1])
18
+ return sortedMap.splice(0, k).map((item)=> item[0])
19
+};
0 commit comments