Given an array of integers and an integerk, find out whether there are two distinct indicesiandjin the array such thatnums[i] = nums[j]and the difference betweeniandjis at mostk.
Subscribeto see which companies asked this question
Hide Tags ArrayHash Table Hide Similar Problems (E) Contains Duplicate(M) Contains Duplicate IIIpublic class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { Setset = new HashSet (); for(int i=0;i k) set.remove(nums[i-k-1]); if(!set.add(nums[i])) return true; } return false; } }