C# Solution using dictionary


  • 0
    E
        public int RemoveDuplicates(int[] nums) {
         
            Dictionary<int,int> dict= new Dictionary<int,int>();
            int count=0;
            int newLength=0;
            for(int i=0;i<nums.Length;i++)
            {
                if(!dict.ContainsKey(nums[i]))
                {
                    dict.Add(nums[i],1);
                }
                else
                {
                    count=dict[nums[i]];
                    count++;
                    dict[nums[i]]=count;
                }
                
            }
            foreach(KeyValuePair<int,int> kvp in dict)
            {
                  nums[newLength]=kvp.Key;
                    newLength++;
               
            }
            return newLength;
        }
    }

Log in to reply
 

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