I am trying to practice basic algorithm and am keep receiving segmentation error while running on my mac vscode.
Can any one help?
#include <iostream>
#include <vector>
using namespace std;
int removeDuplicates(vector<int> &nums)
{
vector<int>::iterator itr = nums.begin();
int prev = *itr;
for(itr = ++itr ; itr != nums.end() ; itr++) {
if(*itr == prev) {
nums.erase(itr);
} else {
prev = *itr;
}
}
itr = nums.begin();
for(; itr != nums.end(); itr++) {
cout<<*itr<<endl;
}
return 1;
}
using namespace std;
int main() {
vector<int> test1;
test1.push_back(1);
test1.push_back(1);
test1.push_back(1);
test1.push_back(2);
test1.push_back(2);
test1.push_back(3);
test1.push_back(3);
test1.push_back(3);
removeDuplicates(test1);
}
The problem in the terminal is [Done] exited with code=139 in 0.59 seconds, where code=139 seems to imply the segmentation error.
Is this the problem with my local running environment or is it the problem with the logic?
Any help would be appreciated.