Skip to content

Latest commit

 

History

History
16 lines (16 loc) · 339 Bytes

剑指II060出现频率最高的 k 个数字.md

File metadata and controls

16 lines (16 loc) · 339 Bytes
func topKFrequent(nums []int, k int) []int {
    var res []int
    counts := make(map[int]int)
    for _, n := range nums {
        counts[n]++
    }
    for n := range counts {
        res = append(res, n)
    }
    sort.Slice(res, func(i, j int) bool {
        return counts[res[i]] > counts[res[j]]
    })
    return res[:k]
}