Simple JS solution


  • 0
    R
    var removeDuplicates = function(A) {
        if(A.length <= 2)
            return A.length;
        reformedIndex = 1; 
        doubled = false;
        for(i = 1; i < A.length; i++) {
            if(A[i] !== A[i-1]) {
                doubled = false;
                A[reformedIndex++] = A[i];
            } else if(!doubled) {
                doubled = true;
                A[reformedIndex++] = A[i];
            }
        }
        A.splice(reformedIndex, A.length);
        return A.length;
    };

Log in to reply
 

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