topKFrequent = function(nums, k) {
    const mp = new Map();
    const arr = new Array(nums.length + 1).fill(0);
    const ans = [];

    nums.forEach(el => {
        const val = mp.get(el) || 0;
        mp.set(el, val + 1);
    });

    for ( let [key, value] of mp ) {
        const prev = arr[value] || [];
        prev.push(key);
        arr[value] = prev;
    }


    arr.reverse();
    for (let el of arr) {
        if (k < 1) break;
        if (el) {
            for (let el2 of el) {
                ans.push(el2);
                k--;
            }
        }
    }

    return ans;
}
Without Sort
Time O(N) | Space O(k)
https://leetcode.com/problems/top-k-frequent-elements/
@param {number[]} nums
@param {number} k
@return {number[]}