/**
* 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.
*/
import java.util.HashSet;
import java.util.Set;
import java.util.HashMap;
import java.util.Map;
public class ContainsDuplicatesII219 {
// too slow
public boolean containsNearbyDuplicate(int[] nums, int k) {
if (nums.length < 2) {
return false;
}
for (int i = 0; i < nums.length; i++) {
int numI = nums[i];
for (int j = i + 1; j < nums.length && j - i <= k; j++) {
int numJ = nums[j];
if (numI == numJ) {
return true;
}
}
}
return false;
}
public boolean containsNearbyDuplicate2(int[] nums, int k) {
if (nums.length < 2) {
return false;
}
Map