I was attempting an easy problem that asked me given a std::vector of integers and a value, remove all elements from that vector with that value and return the size of the resulting vector. The code I ended up writing was
int removeElement(vector<int>& nums, int val) {
for (int i = 0; i < nums.size(); ) {
if (nums[i] == val) {
nums.erase(nums.begin() + i);
} else {
i++;
}
}
return nums.size();
}
However, after going through my code, is this solution actually O(n^2) since I'm deleting elements in the vector inside a loop?