allensuvorov / leetcodeProblems

0 stars 0 forks source link

219. Contains Duplicate II #9

Closed allensuvorov closed 1 year ago

allensuvorov commented 1 year ago

Looking for a better solution, than nested loop over a subarray.

allensuvorov commented 1 year ago

func containsNearbyDuplicate(nums []int, k int) bool { hm := map[int]int{} for i, v := range nums { if _, ok := hm[v]; !ok { hm[v] = i continue } if i - hm[v] <= k { return true } else { hm[v] = i } } return false }