Best C# Solution


  • 0
    K
        public bool ContainsNearbyDuplicate(int[] nums, int k)
        {
            if (nums.Count() == 0 || k  == 0)
                return false;
            
            var dict = new Dictionary<int,int>();
            for(int i = 0; i < nums.Count(); ++i){
                if (dict.ContainsKey(nums[i])){
                    if (i - dict[nums[i]] <= k)
                        return true;
                    dict.Remove(nums[i]);
                }
                
               dict[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.