public static int removeElement(int[] A, int elem) {
    	int len = A.length;
        for (int i = 0; i < len; i++) {
        	while (len > 0 && elem == A[len - 1]) {
        		len = len - 1;
        	}
        	if (len <= i) return len;
        	if (elem == A[i]) {
        		A[i] = A[len - 1];
        		len = len - 1;
        	}
        }
    	return len;
    }