Simple AC solution in C++ with O(n) time and O(1) space


  • 6
    C
    class Solution {
    public:
        int removeDuplicates(int A[], int n) {
            int length=n, i,j;
            for(i=1,j=0;i<n;i++){
                if(A[j]<A[i]) A[++j]=A[i];
                else length--;
            }
            return length;
            
        }
    };

Log in to reply
 

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