Java O(n) solution


  • 0
    P
    public int removeDuplicates(int[] nums) {
        int count = 0;
        int i = 0 ;
        while( i < nums.length ){
            if(i + 1 < nums.length &&  nums[i] == nums[i+1]){
                nums[count++] = nums[i];
                nums[count++] = nums[i];
                int k = nums[i];
                i = i + 2;
                while(i < nums.length && k == nums[i]){
                    i++;
                }
            }else{
                nums[count++] = nums[i];
                i++;
            }
        }
        return count;
    }

Log in to reply
 

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