var empty = struct{}{}func uniqueOccurrences(arr []int) bool { // map[数字]次数 count := make(map[int]int, len(arr)) for _, num := range arr { count[num]++ } // set[次数] times := make(map[int]struct{}, len(count)) for _, c := range count { if _, has := times[c]; has { return false } times[c] = empty } return true}
独一无二的出现次数 - 数组模拟 map
func uniqueOccurrences(arr []int) bool { // index: 数字 // value: 次数 // 2002: -1000 <= arr[i] <= 1000 count := make([]int, 2002) for _, num := range arr { count[num + 1000]++ } // index: 次数 // value: 出现过 // 1002: 1 <= arr.length <= 1000 times := make([]bool, 1002) for _, c := range count { if c == 0 { continue } if times[c] { return false } times[c] = true } return true}