Given an array of integers and an integer k, find out whether there there are two distinct indices i and j in the array such that nums[i] = nums[j] and the difference between iand j is at most k.
public class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Hashtable <Integer, List<Integer>> table = new Hashtable<Integer, List<Integer>>();
for (int i = 0; i < nums.length; i++) {
if (table.containsKey(nums[i])) {
List<Integer> tmp = table.get(nums[i]);
for (int j = 0; j < tmp.size(); j++) {
if (tmp.get(j) + k >= i) return true;
}
tmp.add(i);
}
else {
List<Integer> tmp = new ArrayList<Integer>();
tmp.add(i);
table.put(nums[i], tmp);
}
}
return false;
}
}