So I want to compare the 0th element of a vector with the other elements to see if they're equal because I want to remove other instances of that element's value from the vector e.g. {1, 1, 2, 3, 1} becomes {1, 2, 3} and this is the code I wrote:
std::vector<int> arr = {1,1,5,5,1,1};
for (int k = 1; k < arr.size(); k++)
{
if(arr[0] == arr[k]) {
arr.erase(arr.begin() + k);
}
The output I expected from this was:
155
Since it's supposed to remove all instances of the 1 except the first one, but what I instead get is:
1551
Where'd the last 1 come from and how do I fix this?