Two Pointer Is Enough (Java Solution)


  • 0
    S

    Keep two pointers. One for current valid last value, another for possible candidates. Keep comparing possible candidates with last value.

    Read the code and you'll know

    public int removeDuplicates(int[] A) {
            if(A.length == 0){return 0;}
            int i, j;
            i = 1;
            j = 0;
            while(i < A.length){
                if(A[i] == A[j]){
                    i++;
                }else{
                    A[++j]=A[i++];
                }
            }
            return j+1;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.