Simple and clear C++ in place solution


  • 0
    Z
    int removeDuplicates(int A[], int n) {
        if(n == 0){
            return 0;
        }
        int p = 0;
        int dupTimes = 0;
        for(int i = 1; i < n; i++){
            if(A[i] != A[p]){
                p++;
                A[p] = A[i];
                dupTimes = 0;
            }
            else{
                dupTimes++;
                if(dupTimes == 1){
                    p++;
                    A[p] = A[i];
                }
            }
        }
        return p + 1;
    }

Log in to reply
 

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