
g0301_0400.s0347_top_k_frequent_elements.solution.ts Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-all Show documentation
Show all versions of leetcode-in-all Show documentation
104 LeetCode algorithm problem solutions
The newest version!
// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Sorting
// #Heap_Priority_Queue #Counting #Divide_and_Conquer #Quickselect #Bucket_Sort
// #Data_Structure_II_Day_20_Heap_Priority_Queue #Big_O_Time_O(n*log(n))_Space_O(k)
// #2023_10_04_Time_62_ms_(87.48%)_Space_45.6_MB_(67.84%)
function topKFrequent(nums: number[], k: number): number[] {
let elementCount = new Array(nums.length)
let hashMap = new Map()
let res: number[] = new Array()
nums.forEach((num) => {
if (!hashMap.has(num)) hashMap.set(num, 1)
else hashMap.set(num, hashMap.get(num)! + 1)
})
hashMap.forEach((value, key) => {
if (elementCount[value] === undefined) elementCount[value] = [key]
else elementCount[value].push(key)
})
for (let i = elementCount.length - 1; i >= 0; i--) {
if (elementCount[i] !== undefined) res.push(...elementCount[i].values())
if (res.length === k) {
return res
}
}
return res
}
export { topKFrequent }
© 2015 - 2025 Weber Informatics LLC | Privacy Policy