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