Open In App

How to Find Union of Two Vectors in C++?

Last Updated : 29 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The union of two vectors refers to the collection of all the distinct elements from both vectors. In this article, we will explore different methods to find the union of two vectors in C++

The most efficient way to find the union of two sorted vectors is by storing all elements of the two vectors into a set container. Let’s take a look at an example:

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v1 = {1, 7, 6, 6, 3};
    vector<int> v2 = {2, 5, 8, 4, 5};

    // Insert elements from both vectors to set
    set<int> res(v1.begin(), v1.end());
    res.insert(v2.begin(), v2.end());

    for (auto i : res)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 6 7 8 

Explanation: The set automatically stores only unique elements, but it maintains a sorted order so we may alter the order of the original elements.

Note: This method can also use unordered_set container, but it will still not preserve the original order of the elements.

C++ provides a few more methods to find the union of two vectors in C++. Some of them are given below:

Using set_union()

The set_union() function finds the union of the two given sorted range so first sort the two vectors using sort() function.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v1 = {1, 3, 6, 6, 7};
    vector<int> v2 = {2, 4, 5, 5, 8};

  	// Resultant vector to store union
    vector<int> res;

    // Finding union
    set_union(v1.begin(), v1.end(), v2.begin(),
              v2.end(), inserter(res, res.begin()));

    // Remove duplicates
    res.erase(unique(res.begin(), res.end()), res.end());

    for (auto i : res)
        cout << i << " ";
    return 0;
}

Output
1 2 3 4 5 6 7 8 

Explanation: This method also adds the duplicates to the union. So, we have to cleanse it by removing the duplicates using unique() function.

Using Memoization

The idea is to traverse the two vectors starting from the beginning while adding the elements first encountered to the resultant union vector. The encountered elements can be tracked using an unordered_set container.

C++
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v1 = {1, 3, 6, 6, 7};
    vector<int> v2 = {2, 4, 5, 5, 8};

    vector<int> res;
    unordered_set<int> seen;

    // Iterate through the first vector
    for (int i : v1) {
      
      	// Add i if not already seen and mark
      	// it as seen
        if (seen.find(i) == seen.end()) {
            res.push_back(i);  
            seen.insert(i);
        }
    }

    // Iterate through the second vector
    for (int i : v2) {
      
      	// Add i if not already seen and mark
    	// it as seen
        if (seen.find(i) == seen.end()) {
            res.push_back(i);
            seen.insert(i);
        }
    }

    for (auto i : res)
        cout << i << " ";
    return 0;
}

Output
1 3 6 7 2 4 5 8 





Next Article
Article Tags :
Practice Tags :

Similar Reads