Two pointer approach ,using erase function


#1
int Solution::removeDuplicates(vector<int> &A) {
   int n = A.size();
   int r =n;
   int i =0,j = 1,x=1;
   while(i<r-1&&j<r){
       if(A[i]==A[j]){x++;j++;}
       else{
           if(x>2){A.erase(A.begin()+i,A.begin()+x-2+i);i += 2 ;j = i+1;r-=(x-2);}
           else if(x==2) {i+=2;j = i+1;}
           else {i++;j=i+1;}
           x = 1;
       }
   }
   if(x>2)A.erase(A.begin()+i,A.begin()+i+x-2);
    
    return A.size();
    
}.