My Hashtable sollution


  • 0
    C

    public class Solution {
    public boolean containsNearbyDuplicate(int[] nums, int k)
    {

     int n = nums.length;
     
     HashMap<Integer, Integer> map = new HashMap<>();
     int dif; 
     for(int i = 0; i < n ; i++ )
      
      {
          if(i>k) map.remove(nums[i-k-1]);
          if(!map.containsKey(nums[i]))map.put(nums[i],i);//using hashmap for comparision 
          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.