Share my simple Java solution


  • 0
    F
    public class Solution {
        public boolean containsNearbyDuplicate(int[] nums, int k) {
            Map<Integer,Integer> hm = new HashMap();
            for (Integer i = 0; i < nums.length; i++) {
                Integer prev = hm.put(nums[i], i);
                if (prev != null && (i - prev) <= k  ) return true;
            }
            return false;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.