AC Java solution using HashMap

  • 0

    First time sharing my code, any comment is welcome. Thank you so much.

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

Log in to reply

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