public int removeElement(int[] nums, int val) {
        int index = 0;
        for (int i = 0; i < nums.length; ++i) {
            while (i < nums.length && nums[i] == val)
                ++i;
            if (i >= nums.length)
                break;
            nums[index++] = nums[i];
        }
        return index;
    }