O(N) in place solution


  • 5
    S
    int removeDuplicates(int A[], int n) {
        if (n <= 2) return n;
        int repeat = 0, count = 1;
        for (int i = 1; i < n; ++i) {
            if (A[i] == A[i-1] && repeat < 1) {
                A[count++] = A[i];
                repeat++;
            }
            else if (A[i] > A[i-1]) {
                A[count++] = A[i];
                repeat = 0;
            }
        }
        
        return count;
    }

Log in to reply
 

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