funccontainsNearbyDuplicate(nums []int, k int) bool { m :=make(map[int]int)for i,v :=range nums {if _, found := m[v]; found {if i - m[v] <= k {returntrue } } m[v] = i }returnfalse}
Solution 2
funccontainsNearbyDuplicate(nums []int, k int) bool { m :=make(map[int]bool, len(nums))for i,v :=range nums {if _, found := m[v]; found {returntrue } m[v] =trueiflen(m) == k+1 {delete(m, nums[i-k]) } }returnfalse}