My java hashtable solution


  • 0
    C
       import java.util.Hashtable;
       public class Solution {
        public boolean containsNearbyDuplicate(int[] nums, int k) {
        //create the hashtable
        Hashtable<Integer,Integer> table = new Hashtable();
        int length = nums.length;
        
        for(int i=0;i<length;i++)
        {
            Integer position = 0;
            if((position = table.get(nums[i]))==null)
            {
                table.put(nums[i],i);
            }
            else
            {
                if(i-position<=k)
                return true;
                
                table.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.