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 0a3990a commit 0e27667Copy full SHA for 0e27667
top-k-frequent-elements/dongzoolee.ts
@@ -0,0 +1,15 @@
1
+function topKFrequent(nums: number[], k: number): number[] {
2
+ const mp: { [k: number]: number } = {};
3
+ nums.forEach((n) => (typeof mp[n] === "number" ? mp[n]++ : (mp[n] = 1)));
4
+
5
+ const unq_nums = [...new Set(nums)];
6
+ unq_nums.sort((a, b) => {
7
+ if (mp[a] === mp[b]) {
8
+ return 0;
9
+ }
10
11
+ return mp[a] < mp[b] ? 1 : -1;
12
+ });
13
14
+ return unq_nums.slice(0, k);
15
+}
0 commit comments