public boolean containsNearbyDuplicate(int[] a, int k) {
        Map<Integer, Integer> map= new HashMap<>();
        for(int i=0; i<a.length; i++){
            if(map.containsKey(a[i]) && i- map.get(a[i]) <=k)return true;
            map.put(a[i], i);
        }

        return false;
    }
Maintain every element with last encountered index in the array. In each loop iteration check if
this element is in map and check last encountered index to decide.
src/problems/easy/ContainsDuplicateII.java