@@ -0,0 +1,1215 @@
+package easy;%0A%0Aimport java.util.HashMap;%0Aimport java.util.Map;%0A%0A/**219. Contains Duplicate II  QuestionEditorial Solution  My Submissions%0ATotal Accepted: 69920%0ATotal Submissions: 228733%0ADifficulty: Easy%0AGiven 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%5Bi%5D = nums%5Bj%5D and the difference between i and j is at most k.*/%0Apublic class ContainsDuplicateII %7B%0A    //pretty straightforward, use a hashmap, key is the number itself, value is the last index that this value appeared in the array%0A    //we can keep updating the value as we move forward, since if the current index minus the last index cannot be smaller than k, then%0A    //the later indices won't even do either. So, we only need to keep one index in the value of the HashMap. Cheers!%0A    public boolean containsNearbyDuplicate(int%5B%5D nums, int k) %7B%0A        Map%3CInteger, Integer%3E map = new HashMap();%0A        for(int i = 0; i %3C nums.length; i++)%7B%0A            if(map.containsKey(nums%5Bi%5D)) %7B%0A                if(i - map.get(nums%5Bi%5D) %3C= k) return true;%0A                else map.put(nums%5Bi%5D, i);%0A            %7D%0A            else map.put(nums%5Bi%5D, i);%0A        %7D%0A        return false;%0A    %7D%0A%7D%0A

contains duplicate II