-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0347-top-k-frequent-elements.ts
51 lines (44 loc) · 1.28 KB
/
0347-top-k-frequent-elements.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
function topKFrequent(nums: number[], k: number): number[] | undefined {
const hash: {
[key: number]: number;
} = {};
const freq: number[][] = Array.apply(null, Array(nums.length + 1)).map(
() => []
);
nums.forEach((item) => {
if (hash[item]) {
hash[item]++;
} else {
hash[item] = 1;
}
});
Object.keys(hash).forEach((item) => {
const key = Number(item);
const value = hash[item];
freq[value].push(key);
});
const res: number[] = [];
for (let i = freq.length - 1; i >= 0; i--) {
const element = freq[i];
for (let j = 0; j < element.length; j++) {
const second = element[j];
res.push(Number(second));
if (res.length == k) {
return res;
}
}
}
}
function topKFrequentNLogN(nums: number[], k: number): number[] {
const map = nums.reduce((acc, num) => {
const currentNumber = acc[String(num)];
acc[num] = currentNumber ? currentNumber + 1 : 1;
return acc;
}, {});
return Object.entries(map)
.sort(
([, countA], [, countB]) => (countB as number) - (countA as number)
)
.slice(0, k)
.map(([num]) => +num);
}