Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
Solution
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
if(nums==null || nums.length<2 || k==0)
return false;
int i=0;
HashSet<Integer> set = new HashSet<Integer>();
for(int j=0; j<nums.length; j++){
if(!set.add(nums[j])){
return true;
}
if(set.size()>=k+1){
set.remove(nums[i++]);
}
}
return false;
}
}