![]() |
In C++, vectors are containers that can change their size dynamically during the runtime. The union of two vectors refers to the collection of all the distinct elements from the two original vectors. In this article, we will learn how we can find the union of two sorted vectors in C++. Example: Input: myVector1 ={1,2,3,4,5} myVector2 = {4,5,6,7,8} Output: Union of vectors = {1,2,3,4,5,6,7,8} Finding Union of Two-Sorted Vectors in C++To find the union of two sorted std::vector in C++, we can use a std::set container to store the elements of both vectors so that the repeated elements from both vectors will be stored only once and then we can copy back the elements into a resultant vector which will be the union of the given two vectors. C++ Program to Find the Union of Two Sorted VectorsC++
Output
Vector 1: 1 2 3 4 5 Vector 2: 4 5 6 7 8 Union of Vectors: 1 2 3 4 5 6 7 8 Time complexity : O(N + M ), where N is the number of elements in vector 1 and M is number of elements in vector 2 |
Reffered: https://www.geeksforgeeks.org
C++ |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 12 |