C# - using value to index hash table


  • 0
        public bool ContainsNearbyDuplicate(int[] nums, int k) 
        {
            Dictionary<int, int> map = new Dictionary<int,int>();
            for (int i = 0; i < nums.Length; i++)
            {
                if (map.ContainsKey(nums[i]) && i - map[nums[i]] <= k)
                {
                    return true;
                }
                map[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.