Easy solution but ganda likha hai


#1

int Solution::removeDuplicates(vector &A) {
// Do not write main() function.
// Do not read input, instead use the arguments to the function.
// Do not print the output, instead return values as specified
// Still have a doubt. Checkout www.interviewbit.com/pages/sample_codes/ for more details

int i=0;
int j=0;
while(i<A.size()-1 && j<A.size()-1)  // jab tak i ya j last second tak nhi pahuch jaate. i<A.size() isliye nhi likha coz if i==A.size()-1 hota to much kaam ka nhi hota
{
    while(A[i]==A[j])
    {  
        if(j==A.size()-1) // agar j last position pe pahuch gaya to j++ krke A[i] , A[j] ke equal to nhi hai iski problem aati so issue accha simple break kra lu; last position me hone ke baawjood j++ isliye kra rha hu coz mujhe erase bhi to krna hai that's why to satisfy erase function j++;
        {
            j++;
            break;
        }
        j++;
    }
    
    if(i+1!=j)
    {
        if( i+2<A.size() && A[i]==A[i+2]) agar same element ka cnt 2 see jyada hai tabhi dlt kra do  
        {
            A.erase(A.begin()+i+2,A.begin()+j);
        }
        else
        {
            i++; // i ko 2 see badhana hai
            
        }
        
    }
    i++;
    j=i;
}
return A.size();

}