219. Contains Duplicate II
Input: nums = [1,2,3,1], k = 3
Output: trueInput: nums = [1,0,1,1], k = 1
Output: trueInput: nums = [1,2,3,1,2,3], k = 2
Output: falsepublic class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i]) && (i - map.get(nums[i]) <= k)) return true;
map.put(nums[i], i);
}
return false;
}
}Last updated