Contains Duplicate II

ID: 219; easy

Solution 1

func containsNearbyDuplicate(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 {
                return true
            }
        }
        m[v] = i
    }
    return false
}

Solution 2

Maintain a map of k elements

Last updated

Was this helpful?