```
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
a = {}
for i in range(len(nums)):
if nums[i] not in a:
a[nums[i]] = i
else:
if abs(i - a[nums[i]]) <= k:
return True
else:
a[nums[i]] = i
return False
```