Remove Element

Given an array and a value, remove all instances of that value in place and return the new length.

The order of elements can be changed. It doesn't matter what you leave beyond the new length.

用两个指针i,j,遍历数组,j指向最近一个等于val的元素,i向后循环移位,发现非Val元素后与j所指的元素交换位置。 最后j的位置就是新数组的长度

public int removeElement(int[] nums, int val) {
    int j = 0;
    for (int i = 0; i < nums.length; i++) {
        if (nums[i] == val) {
            continue;
        }
        nums[j++] = nums[i];
    }
    return j;
}