My 1ms Java solution


  • 1
    J

    **

    public class Solution {
        public int removeDuplicates(int[] nums) {
            if(nums.length == 0 || nums.length == 1){
                return nums.length;
            }
            int read = 2;
            int write = 2;
            int count = 1;
            
            while(read < nums.length){
                if(nums[read] == nums[read-1]){
                    if(nums[read] == nums[write-2]){
                        read++;
                    }
                    else{
                        nums[write] = nums[read];
                        read++;
                        write++;
                    }
                }
                else{
                nums[write] = nums[read];
                read++;
                write++;
                }
            }
            return write;
        }
    }
    

    **


Log in to reply
 

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