C# solution using Dictionary<int, int>


  • 2
    F
    public class Solution {
        public bool ContainsNearbyDuplicate(int[] nums, int k) {
            var dic =new Dictionary<int, int>();
            for(int i=0;i<nums.Length;i++){
                if(!dic.ContainsKey(nums[i]))
                    dic.Add(nums[i], i);
                else
                    {
                        if(i-dic[nums[i]]<=k)
                            return true;
                        else
                            dic[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.