Simple Solution in C#


  • 0
    P
    public class Solution {
        public int RemoveDuplicates(int[] nums) {
             if (nums.Length <= 2)
                    return nums.Length;
              
                int j = 1;
                int counter = 1;
    
                for (int i = 1; i < nums.Length; i++)
                {
                    if (nums[i] == nums[i - 1])
                    {
                        if (counter < 2)
                        {
                            counter++;
    
                           nums[j] = nums[i];
                            j++;
                        }
                    }
                    else
                    {
                        counter = 1;
    
                        nums[j] = nums[i];
                        j++;
                    }
                }
    
                return j ;
        }
    }
    

Log in to reply
 

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