My concise java solution.


  • 0
    W

    There is no need to use count. Simply compare the current value with previous 2 values written in the array.

    public class Solution {
    public int removeDuplicates(int[] A) {
        int readIdx = 0;
        int writeIdx = 0;
        
        while(readIdx < A.length)
        {
            if(readIdx < A.length && writeIdx < 2) { writeIdx++;}
            else if(A[readIdx] != A[writeIdx - 2] || A[readIdx] != A[writeIdx-1])
            {
                A[writeIdx] = A[readIdx];
                writeIdx++;
            }
            readIdx++;
        }
        
        return writeIdx;
    }
    

    }


  • 0
    H

    -1: This solution doesn't pass this test case, right?
    Input: [1,1,1,2,2,3]
    Output: [1,1,2,3]
    Expected: [1,1,2,2,3]


Log in to reply
 

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