What wrong with this code?


#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 n=A.size();
int i=0;
while(i<n-1)
{
    if(A[i]==A[i+1])
    {
        A.erase(A.begin()+i);
        n=n-1;
        i--;
    }
    i++;
}

return n;

}


#2

Consider an array [0, 0, 1]. In second iteration of your loop I will be -1. So you’ll be accessing A[-1] which is invalid.