My java solution with two pointers


  • 1
    L
    public int removeDuplicates(int[] nums) {
        if(nums.length <= 2) return nums.length;
        int slow = 1;
        int fast = slow + 1;
        for(;fast < nums.length;fast++){
            if(nums[fast] != nums[slow] || nums[fast] == nums[slow] && nums[slow] != nums[slow-1]){
                nums[++slow] = nums[fast];
            }
        }
        return slow+1;
    }

  • 0
    T

    You can also remove the condition nums[fast] == nums[slow].
    Kudos for the solution!


Log in to reply
 

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