nums๋ ์ซ์๋ก ์ด๋ฃจ์ด์ง ๋ฐฐ์ด์
๋๋ค.
๊ฐ์ฅ ์์ฃผ ๋ฑ์ฅํ ์ซ์๋ฅผ k ๊ฐ์๋งํผ returnํด์ฃผ์ธ์.
nums = [1,1,1,2,2,3]
k = 2
return [1,2]
nums = [1]
k = 1
return [1]
function topK(nums, k) {
let obj = {}
for (let i in nums) {
nums[i] in obj ? obj[nums[i]] += 1 : obj[nums[i]] = 1
}
let sorted = Object.keys(obj).sort(function(a, b) {
return obj[b] - obj[a]
})
return sorted.slice(0, k).map(x => parseInt(x))
}
topK([1,1,1,1,2,2,3],2)