알고리즘 공부 2020. 8. 20. 22:16

https://leetcode.com/problems/top-k-frequent-elements/

 

Top K Frequent Elements - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number[]}
 */
var topKFrequent = function(nums, k) {
    const res = {}, hash={};
    
    for( var i=0; i<nums.length; ++i )
        hash[ nums[i] ] = 0;    
    
    var keys = Object.keys(hash);
    for( var i=0; i<nums.length; ++i )
        ++hash[ nums[i] ];

    
    keys.sort( (b,a)=>{ return hash[a]-hash[b]; } );

    return keys.slice(0,k);
};
posted by Sense.J
: