Tag:Array,Hash Table
**Difficulty: **Easy
Description:
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
Code
class Solution(object):
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
Dict = {}
for i in range(len(nums)):
Dict.setdefault(nums[i],[])
if len(Dict[nums[i]]) and i-Dict[nums[i]][-1]<=k:
return True
else:
Dict[nums[i]].append(i)
return False