I've written a simple function that removes elements from a vector (V2), based upon the values of another vector (V1):
std::vector<int> V1={6,2,3,4};
std::vector<int> V2={9,4,8,6,7};
for(int i=0; i<V1.size(); i++)
{
if(!V2.empty())
{
V2.erase(std::remove(V2.begin(),V2.end(),V1[i]),V2.end());
}
}
My challenge is that the above needs to be O(n) complexity. Currently this is O(n*m), n being V1, m being V2.
N.B. Arrays are not and cannot be sorted as the elements original index values are required.
Questions:
Am I right in saying 'V2.erase' is stopping this function from being O(n)? (Because its a nested iteration within the for loop).
Is there a way around this, by performing the erase operation outside the loop?