Thursday, December 4, 2014

Remove Duplicates from Sorted Array II

    int removeDuplicates(int A[], int n) {
        if(n==0) return 0;
        int dup=0;
        int len=1;
        for(int i=1;i<n;i++){
            if(A[i]==A[i-1]){
                dup++;
                if(dup<2)
                    A[len++]=A[i];
            }
            else{
                A[len++]=A[i];
                dup=0;
            }
        }
        return len;
    }

No comments:

Post a Comment