I’ve got vector A which contains numbers 1,7,9,9,3,13,3
I’ve got vector B which contains numbers 9,11,7,7,3,2,1
I need to get vector C which will contain every element from two vectors below but only one time each (for example numbers 9 from vector A shouldn’t repeat)
so C should contain 1,7,9,3,13,11,2
This code will make vector C which will be union of two vectors but there will be some numbers repeating (if one vector includes 3x number 1 then C includes also 3x number 1)
vector<int>union(vector<int>A,vector<int>B)
{
sort(A.begin(),A.end());
sort(B.begin(),B.end());
vector<int> C(A.size()*2); //vector A has same size as vector B
vector<int>::iterator it= set_union(A.begin(),A.end(),B.begin(),B.end(),C.begin());
C.resize(it-C.begin());
return C;
}
It has to be working as fast as possible.
What is the best way to do it so?
The right solution for this problem is to sort each vector first and then get intersection/union/filtering in a way that is similar to the
sinksorting.