Java HashMap easy solution


  • 0
    U
    public class Solution {
        public boolean containsNearbyDuplicate(int[] nums, int k) {
            Map<Integer, Integer> map = new HashMap<>();
            for(int i=0; i< nums.length;i++) {
                if(map.containsKey(nums[i])){ if (Math.abs(i-map.get(nums[i]))<=k) return true;}
                map.put(nums[i], i);
            }
            return false;
        }
    }
    

Log in to reply
 

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