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 difference between
i
and
j
is at most
k
.
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
int [] a =new int [35000];
for(int i = 0;i<35000;i++){
a[i]=-1;
}
for(int i = 0;i<nums.length;i++){
if(a[nums[i]+100]==-1)a[nums[i]+100]=i;else{
if(i-a[nums[i]+100]<=k)return true;
a[nums[i]+100]=i;
}
}
return false;
}
}