Iterate through array and use HashMap to save `number[i]`

as a key and `i`

as a value. If the map already contains `number[i]`

- subtract `map.get(number[i])`

from `i`

and return true if result is less or equal to `k`

.

```
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i]) && (i - map.get(nums[i])) <= k) {
return true;
}
map.put(nums[i], i);
}
return false;
}
```