c# solution with dictionary


  • 0
    K

    public class Solution {
    public bool ContainsNearbyDuplicate(int[] nums, int k) {
    Dictionary<int,int> d = new Dictionary<int,int>();
    for(int n=0;n<nums.Length;n++){
    if(d.ContainsKey(nums[n])){
    if(n-d[nums[n]]<=k)
    return true;
    else
    {
    d.Remove(nums[n]);
    d.Add(nums[n],n);
    }
    }
    else
    d.Add(nums[n],n);
    }

        return false;
    }
    

    }


Log in to reply
 

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