Skip to content

Commit 03a9dc8

Browse files
committed
feat: add Top K Frequent Elements
1 parent 59d537d commit 03a9dc8

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ https://neetcode.io/roadmap
44

55
### Leetcode
66

7-
| # | Title | Difficulty | Solution |
8-
| --- | ----------------------------------------------------------------------- | ---------- | ---------------------------------------------------- |
9-
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | Easy | [TypeScript](./TypeScript/217.contains-duplicate.ts) |
10-
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | Easy | [TypeScript](./TypeScript/242.valid-anagram.ts) |
11-
| 242 | [Two Sum](https://leetcode.com/problems/two-sum/) | Easy | [TypeScript](./TypeScript/1.two-sum.ts) |
12-
| 49 | [Group Anagrams](https://leetcode.com/problems/two-sum/) | Medium | [TypeScript](./TypeScript/49.group-anagrams.ts) |
7+
| # | Title | Difficulty | Solution |
8+
| --- | --------------------------------------------------------------------------------- | ---------- | --------------------------------------------------------- |
9+
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | Easy | [TypeScript](./TypeScript/217.contains-duplicate.ts) |
10+
| 242 | [Valid Anagram](https://leetcode.com/problems/valid-anagram/) | Easy | [TypeScript](./TypeScript/242.valid-anagram.ts) |
11+
| 242 | [Two Sum](https://leetcode.com/problems/two-sum/) | Easy | [TypeScript](./TypeScript/1.two-sum.ts) |
12+
| 49 | [Group Anagrams](https://leetcode.com/problems/two-sum/) | Medium | [TypeScript](./TypeScript/49.group-anagrams.ts) |
13+
| 347 | [Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/) | Medium | [TypeScript](./TypeScript/347.top-k-frequent-elements.ts) |
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
function topKFrequent(nums: number[], k: number): number[] {
2+
const map = new Map<number, number>();
3+
4+
nums.forEach((n) => {
5+
const frequency = map.get(n) ?? 0;
6+
7+
map.set(n, frequency + 1);
8+
});
9+
10+
return Array.from(map)
11+
.sort(([n1, f1], [n2, f2]) => f2 - f1)
12+
.slice(0, k)
13+
.map(([n, f]) => n);
14+
}
15+
16+
function topKFrequent2(nums: number[], k: number): number[] {
17+
const map = new Map<number, number>();
18+
const count: number[][] = new Array(nums.length + 1).fill(0).map(() => []);
19+
const result: number[] = [];
20+
21+
nums.forEach((n) => map.set(n, (map.get(n) ?? 0) + 1));
22+
map.forEach((f, n) => count[f].push(n));
23+
24+
for (let i = count.length - 1; i >= 1; i--) {
25+
for (let j = 0; j < count[i].length; j++) {
26+
result.push(count[i][j]);
27+
28+
if (result.length === k) return result;
29+
}
30+
}
31+
32+
return result;
33+
}

0 commit comments

Comments
 (0)