题目
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
解题思路
nums中字符放进map中,值为字符出现的次数,次数超过1即为重复
代码
func containsDuplicate(nums []int) bool {
if len(nums) < 2 {
return false
}
var numMap map[int]int
numMap = make(map[int]int)
for _, v := range nums {
numMap[v]++
if numMap[v] > 1 {
return true
}
}
return false
}