public static int removeDuplicates(int[] A) {//copied from the answer
        if (A.length <= 2) return A.length;
        int len = A.length;
        int cnt = 0;
        int j = 0;
        for (int i = 1; i < len; ++i) {
            if (A[i] == A[j]) {
                if (cnt == 0) {
                    A[++j] = A[i];
                }
                ++cnt;
            }
            else {
                A[++j] = A[i];
                cnt = 0;
            }
        }
        return j+1;
    }