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 dfe20cb commit 041c4a2Copy full SHA for 041c4a2
top-k-frequent-elements/Totschka.ts
@@ -0,0 +1,15 @@
1
+// https://leetcode.com/problems/top-k-frequent-elements/
2
+function topKFrequent(nums: number[], k: number): number[] {
3
+ const counter = {};
4
+ for (const n of nums) {
5
+ if (!counter[n]) {
6
+ counter[n] = 1;
7
+ } else {
8
+ counter[n] = ++counter[n];
9
+ }
10
11
+ return Object.keys(counter)
12
+ .sort((a, b) => counter[b] - counter[a])
13
+ .map((v) => Number(v))
14
+ .slice(0, k);
15
+}
0 commit comments