# Contains Duplicate II (Leetcode 219)

Problem Link: <https://leetcode.com/problems/contains-duplicate-ii/>

```python
class Solution:
    def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
        
        if(k==0):
            return False
        
        i=0
        j=1
        n=len(nums)
        d={}
        d[nums[i]]=1
        while(j<n):
            
            if(nums[j] in d):
                return True
            else:
                d[nums[j]]=1
            
            if(j-i<k):
                j+=1
                
            else:
                
                if(d[nums[i]]>1):
                    d[nums[i]]-=1
                else:
                    del d[nums[i]]
                
                i+=1
                j+=1
        return False
```
