Given an integer array nums
, return true
if any value appears at least twice in the array, and return false
if every element is distinct.
Given an integer array nums
, return true
if any value appears at least twice in the array, and return false
if every element is distinct.
Solution in Go
func containsDuplicate(nums []int) bool {
set := make(map[int]struct{})
for _, n := range nums {
_, contains := set[n]
if contains {
return true
}
set[n] = struct{}{}
}
return false
}
Get notified once/twice per month when new articles are published.