Intuitive Java Solution


  • 1
    L
    public static int removeElement(int[] nums, int val) {
        int i = 0;
        int j = 0;
        while (j < nums.length) {
            while (j < nums.length && nums[j] == val) j++;
            if (j < nums.length && i != j) nums[i] = nums[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.