注:func中用的是string,如果你们的类型不同请自行修改
func ArrayCountValueTop(arrInit []string, length int) (arrTop []string) {
if len(arrInit) == 0 {
return arrTop
}
arrMap1 := make(map[string]int)
for _, value := range arrInit {
if arrMap1[value] != 0 {
arrMap1[value]++
} else {
arrMap1[value] = 1
}
}
arrMap2 := arrMap1
for _ = range arrMap1 {
var maxCountKey string
var maxCountVal int = 0
for key, val := range arrMap2 {
if val > maxCountVal {
maxCountVal = val
maxCountKey = key
}
}
arrTop = append(arrTop, maxCountKey)
if len(arrTop) >= length {
return arrTop
}
delete(arrMap2, maxCountKey)
}
return arrTop
}