Can anyone tell me what this problem means

• ``````public int removeElement(int[] A, int elem) {
int oldLength = A.length;
int newLength = oldLength;
for (int i = 0;i<oldLength;i++) {
if (A[i] == elem)
newLength--;
}
return newLength;
}``````

• You should count the newLength, and also make the first newLength elements in the array are the values which do not need to be removed.

For example, `A=[0,2,3,2,5] elem=2`, you should return `3` and also make the original A array like `[0,3,5,2,2]`. However, the problem description has announced that order is ok, so your A might be `[5,0,3,2,2]` or whatever.

• This post is deleted!

• "remove all instances of that value in place and return the new length". notice that you should remove the elem in int[] A but not only count and calculate the newLength. in your code, you keep the original array A[], not changing it to a new array

``````public int removeElement(int[] A, int elem) {
int count = 0;
for (int i = 0; i < A.length; ++i) {
if (A[i] == elem) {
++count;
} else if(count > 0) {
A[i - count] = A[i];
}
}
return A.length - count;
}
``````

this is my method, in the `else` statement, replace the element in `A[]` with the element after it.

• This post is deleted!

• Use a variable to store the boundary between two sets of elements. No extra of copy operation is needed.

``````class Solution {
public:
int removeElement(int A[], int n, int elem) {

int boundary = n;

for (int i = 0; i < boundary; ++i) {
if (A[i] == elem) {
while (--boundary > i && A[boundary] == elem)
;

if (boundary == i)
return boundary;
else
std::swap(A[i], A[boundary]);
}
}

return boundary;
}
};``````

• i think when you submit ,the sever doest not only test the RETURN Length,but also test A[]

• traverse the array, when we found A[i]==elem, assign the last number of array(A[length-1]) to A[i], then i--,length--, so at last length will be the answer

``````    int removeElement(int A[], int n, int elem) {

int length = n;
for(int i=0; i<length; i++)
{
if(elem == A[i])
{
A[i] = A[length-1];
--length;
--i;
}
}

return length;
}``````