692. Top K Frequent Words


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



Runtime: 144 ms, faster than 12.50% of TypeScript online submissions for Top K Frequent Words.
Memory Usage: 41.8 MB, less than 75.00% of TypeScript online submissions for Top K Frequent Words.



```
function topKFrequent(words: string[], k: number): string[] {
    //12:38
    const counter = new Map();

    for (const word of words) {
        if (counter.has(word)) {
            counter.set(word, counter.get(word) + 1)
        } else {
            counter.set(word, 1)
        }
    }

    let result = [...counter.entries()].sort((a: [any, any], b: [any, any]): number => {
            if (a[1] == b[1]) {
                if (a[0] > b[0]) {
                    return 1
                } else {
                    return -1
                }
                return 0
            } else {
                if (a[1] < b[1]) {
                    return 1
                } else {
                    return -1
                }
                return 0
            }
        }
    )
    
    return result.slice(0, k).map((item) => {
        return item[0]
    })
    //12:45
    //debug until 12:53
};
```
