2 Pointer solution


#1

int i=0, j=0;
for(;i<A.size() && j<A.size();i++)
{
if(A[i] != B)
{
A[j] = A[i];
j++;

    }
}
return j;

#2

Thanks bro, for this solution.


#3

don’t we need to remove elements after transforming A ??
because if not we can calculate count directly right?
correct me if iam wrong.


#4

I did not get your point. What do you mean by “remove elements after transforming” ? It is clearly mentioned in the problem statement that “It does not matter what is left beyond the expected length”. so we dont need to worry about actually removing the elements.

And you need to modify the array as well because the array is passed by reference to the function. So if you simply count the elements without modifying the array, the test cases will not pass.