Intuitive Java Solution


  • 0
    L
    public static int removeDuplicates(int[] nums) {
        if (nums.length <= 1) return nums.length;
        int i = 0;
        int j = 0;
        while (j < nums.length) {
            if (i != j) nums[i] = nums[j];
            while (j < nums.length - 1 && nums[j + 1] == nums[j]) j++;
            i++;
            j++;
        }
        return nums.length - (j - i);
    }

Log in to reply
 

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