# leetcode347.前k个高频元素

```golang
func topKFrequent(nums []int, k int) []int {
    res:=make(map[int]int)
    s:=make([]int,0)
    for _,n:=range nums{
        v,ok:=res[n]
        if !ok{
            res[n]=1
            s=append(s,n)
        }else{
            res[n]=v+1
        }
    }

    sort.Slice(s,func(i,j int) bool{
        return  res[s[i]]>res[s[j]]
    })
    return s[:k]

}
```
