Assignment 1.3
Remove element
Given an array of nums and a value of val, you need to remove all elements with a value equal to val in situ and return the new length of the array after the removal.
Don't use extra array space, you must modify the input array in place and do it with O (1) extra space.
The order of elements can be changed. You don't need to consider the elements in the array beyond the new length.
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int i = 0;
for (int j = 0; j < nums.size(); ++j)
{
if (nums.at(j) != val)
{
nums.at(i) = nums.at(j);
++i;
}
}
return i;
}
};