1 public int removeElement(int[] nums, int val) { 2 int last = nums.length - 1; 3 for (int i = 0; i <= last && last >= 0; i++) { 4 while (last >= 0 && nums[last] == val) last--; 5 if (last >= 0 && i < last && nums[i] == val) { 6 nums[i] = nums[last]; 7 last--; 8 } 9 } 10 return last + 1; 11 }