"Partial pass" even conciser than the "for loop" method


  • 0

    Two pointer from two ends

    public int removeElement(int[] nums, int val) {
            int i = 0, j = nums.length-1;
            while (i <= j) {
                if (nums[i] != val) i++;
                else nums[i] = nums[j--];
            }
            return i;
        }
    

Log in to reply
 

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