0

Here is my function code fragment that checks whether two arrays have distinctly common elements and returns a new vector with those elements. However there is a run time error, I do not see it and cannot fix it. I learned how to use the unique function from here:

Checking for duplicates in a vector

vector<int> distinctCommonElements(vector<int>&x, vector<int>&y)
{
vector<int>z;

for(vector<int>::iterator itx=x.begin(); itx<x.end(); itx++)
{
    int ctr=0; 
    int memory=0;
    for(vector<int>::iterator ity=y.begin(); ity<=y.end(); ity++)
    {
        if(*itx==*ity&&ctr==0)
        {
            ctr++;
            z.push_back(*itx);
        }
    }
}
//inspiration from stack overflow and the c++ reference for unique algorithm
sort(z.begin(),z.end()); //sort due to the implementation of the unique algorithm
z.erase(unique(z.begin(),z.end()), z.end()); //unique checks adjacent elements, hence sort first, 
//hence answer always sorted
return z;

}

Blastfurnace
  • 18,411
  • 56
  • 55
  • 70

0 Answers0