6 lines Python solution


  • 3
    N
    class Solution(object):
        def containsNearbyDuplicate(self, nums, k):
            h = {}
            for i, num in enumerate(nums):
                if num in h and i - h[num] <= k:
                    return True
                h[num] = i
            return False
    

  • 0
    S
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.