Please anyone explain what is wrong with my code i m not able to find it

int i;
for(i=0; i<A.size()-1; i++)
{
while(A[i]==A[i+1])
{
A.erase(A.begin()+i);
}
}
return A.size();

This will throw a segfault as A.size() keeps changing as you keep erasing. So i will try to access memory locations that you have removed

Instead “while” use “if” loop. This will resolve run time error.

Just few modications which will avoid runtime error.

while( i < A.size()-1 && A[i]==A[i+1] )
{
A.erase( A.begin() +i +1 );
}

Click here to start solving coding interview questions