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