sundeepblue
5/1/2014 - 10:36 PM

[array] remove duplicates from sorted array. duplicates are allowed at most twice. return the new length

[array] remove duplicates from sorted array. duplicates are allowed at most twice. return the new length

// can be extended to "allow at most T times".
int remove_duplicates(int A[], int N) {
    if(N <= 2) return;
    int tail = 2;
    for(int i=2; i<N; i++) {
        if(A[i] != A[i-2]) {
            A[tail++] = A[i];
        }
    }
    return tail;
}