public int removeDuplicates(int[] A) {
        if(A == null || A.length == 0) return 0;
        if(A.length == 1) return 1;
        int num = 2;
        
        for(int i = 2; i < A.length; i++){
            if(!(A[i] == A[i-1] && A[i] == A[i-2])) num++;
        }
        
        int pos = 2, pre = A[0];
        for(int i = 2; i < A.length; i++){
            if(A[i] != A[i-1] || A[i] != pre){
                pre = A[i-1]; // very interesting! Can you write it correct next time?
                A[pos++] = A[i];
            }
            else pre = A[i-1];
        }
        return num;
    }