9ms Simple Java Solution


  • 2
    E
    public class Solution {  
     public boolean containsNearbyDuplicate(int[] nums, int k) {  
        Set<Integer> s=new HashSet<Integer>();
            for(int i=0;i<nums.length;i++){  
                if(!s.add(nums[i])){  
                    if(i<=k) return true;
                    else{  
                        for(int j=i-k;j<i;j++)
                            if(nums[j]==nums[i]) return true;
                    }  
                }  
            }  
            return false;  
        }  
    }

Log in to reply
 

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