Bootstrap

Golang | Leetcode Golang题解之第219题存在重复元素II

题目:

题解:

func containsNearbyDuplicate(nums []int, k int) bool {
    set := map[int]struct{}{}
    for i, num := range nums {
        if i > k {
            delete(set, nums[i-k-1])
        }
        if _, ok := set[num]; ok {
            return true
        }
        set[num] = struct{}{}
    }
    return false
}
;