public class Solution {

```
public boolean containsNearbyDuplicate(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return false;
}
HashSet<Integer> set = new HashSet<Integer>();
for (int i = 0; i < nums.length; i++) {
if (!set.add(nums[i])) {
if (i<k) {
return true;
}
for (int j = i-k; j < i; j++) {
if (nums[j] == nums[i]) {
return true;
}
}
}
}
return false;
}
```

}