```
class Solution(object):
def containsNearbyDuplicate(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: bool
"""
from collections import Counter
num0=Counter(nums)
for i in num0:
if num0[i]<2:
continue
else:
base=nums.index(i)
counter=1
while counter<num0[i]:
found=nums.index(i,base+1)
if found-base<=k:
return True
base=found
counter+=1
return False
```