Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]
var topKFrequent = function(nums, k) {
var obj ={}
for(let i=0;i<nums.length;i++){
if(!obj[nums[i]]) {
obj[nums[i]] = 1
}else{
obj[nums[i]] +=1
}
}
console.log(Object.keys(obj), obj)
let sortedKeys = Object.keys(obj).sort((a,b)=>obj[b]-obj[a]);
console.log(sortedKeys)
let results =[]
for(let i = 0; i < k; i++){
results.push(sortedKeys[i])
}
return results
};
console.log(topKFrequent([4,1,1,1,2,2], 2))