10000 1 nov · khemssharma/leetcode@fd36191 · GitHub
[go: up one dir, main page]

Skip to content

Commit fd36191

Browse files
committed
1 nov
1 parent fa129f3 commit fd36191

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

JavaScript/bucketSort.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var topKFrequent = function (nums, k) {
2+
const frequencyMap = new Map()
3+
const buckets = Array.from({
4+
length: nums.length + 1
5+
}, () => [])
6+
const result = []
7+
for (const num of nums) {
8+
frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1)
9+
}
10+
for (const [num, freq] of frequencyMap.entries()) {
11+
buckets[freq].push(num)
12+
}
13+
for (let i = buckets.length-1; i >= 0; i--) {
14+
for (const num of buckets[i]) {
15+
result.push(num)
16+
if (result.length === k) {
17+
return result
18+
}
19+
}
20+
}
21+
return result
22+
}

0 commit comments

Comments
 (0)
0