这题比较奇怪,他的test case会检查你是不是把elem真的移走了,我试过用A[i]^elem 去除掉elem,结果就果断不对,但是题目里又说It doesn't matter what you leave beyond the new length.
public int removeElement(int[] A, int elem) {
if(A==null||A.length==0) return 0;
int cnt = 0;
int i = 0;
while(i < A.length) {
if(A[i] != elem) A[cnt++] = A[i++];
else i++;
}
return cnt;
}