5

I'm only using std::vector in this problem, and I can guarantee no duplicates in each vector (but there isn't any order in each vector). How do I union the vectors I have?

Example:

If I have following vectors...

1
1
3 2
5
5 4
2
4
4 2

After the union I should have only two vectors left:

1
2 3 4 5

Again I'm only using vector, std::set isn't allowed.

3
  • 1
    any code written so far? Commented Apr 10, 2013 at 4:21
  • 1
    Please show your code or we can't determine where the problem is. Commented Apr 10, 2013 at 4:22
  • Are you asking how to get the disjoint sets from a number of groups, which contain numbers in same sets? en.wikipedia.org/wiki/Disjoint-set_data_structure Commented Apr 11, 2013 at 7:14

3 Answers 3

14

You can use std::set_union algorithm.

int first[] = {5,10,15,20,25};
  int second[] = {50,40,30,20,10};
  std::vector<int> v(10);                      // 0  0  0  0  0  0  0  0  0  0
  std::vector<int>::iterator it;

  std::sort (first,first+5);     //  5 10 15 20 25
  std::sort (second,second+5);   // 10 20 30 40 50

  it=std::set_union (first, first+5, second, second+5, v.begin());
                                               // 5 10 15 20 25 30 40 50  0  0
  v.resize(it-v.begin());                      // 5 10 15 20 25 30 40 50

Refer :http://www.cplusplus.com/reference/algorithm/set_union/

Sign up to request clarification or add additional context in comments.

2 Comments

Note that a more correct usage would be to pass begin and end iterators in the call to set_union.
I would also recommend using std::back inserter: std::set_union (first.begin(),first.end(),second.begin(),second.end(),std::back_inserter(v));
1

Sort the vectors, then merge them like in mergesort, but don't insert duplicates.

vector<int> a, b, c;
sort( a.begin(), a.end());
sort( b.begin(), b.end());
int i = 0, j = 0;
while( i < a.size() && j < b.size())
if( a[ i ] == b[ j ] )
{
   c.push_back( a[ i ] );
   ++i, ++j;
}
else if( a[ i ] < b[ j ] )
   c.push_back( a[ i++ ] );
else 
   c.push_back( b[ j++ ] );

while( i < a.size()) c.push_back( a[ i++ ] );
while( j < b.size()) c.push_back( b[ j++ ] );

Comments

0

Here is my code:

template<class T> bool vectorExist (vector<T> c, T item)
{
    return (std::find(c.begin(), c.end(), item) != c.end());
}

template<class T> vector<T> vectorUnion (vector<T> a, vector<T> b)
{
    vector<T> c;

    std::sort(a.begin(), a.end());
    std::sort(b.begin(), b.end());

    auto i = a.begin();
    auto j = b.begin();

    while (i != a.end() || j != b.end())
    {
        if (j == b.end() || *i < *j)
        {
            if(!exist(c,*i)) c.insert(*i);
            i++;
        }
        else
        {
            if(!exist(c,*j)) c.insert(*j)
            j++;
        }
    }

    return c;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.