```
public class Solution {
public int removeElement(int[] nums, int val) {
int tail = nums.length - 1;
int i = 0;
for (; i <= tail; i++) {
if (nums[i] == val) nums[i--] = nums[tail--];
}
return i;
}
}
```