Javascript Solution 119 ms beats 100.00 %


  • 0
    T
    /**
     * @param {number[]} nums
     * @param {number} k
     * @return {number[]}
     */
    var topKFrequent = function(nums, k) {
        let dp = {}
        for (let i in nums) {
            let temp = nums[i]
            if (dp[temp]) {
                dp[temp]++
            } else {
                dp[temp] = 1
            }
        }
        let arr = Object.entries(dp).sort((a, b) => b[1] - a[1])
        let result = []
        for (let i = 0; i < arr.length && i < k; i++) {
            result.push(Number(arr[i][0]))
        }
        return result
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.