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