Open In App

How to Find the Intersection of Two Maps in C++?

Last Updated : 28 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, a map is a container that stores elements in the form of key and value pairs. Intersection means the elements that are common between two datasets. In this article, we will learn how to find the intersection of two maps in C++ STL.

Example:

Input:
map1 = {{“apple”, 1}, {“banana”, 2}, {“cherry”, 3}}; 
map2 = {{“banana”, 2}, {“cherry”, 3}, {“date”, 4}};

Output:
Intersection= {{“banana”, 2}, {“cherry”, 3}}

Finding the Intersection of Two Maps in C++

There is no direct function to find the intersection of two maps in C++. But we can create a custom function that compares the key and values of pairs of two maps and if they match, it stores them to another map. It keeps doing this till all the elements of both the maps are scanned.

Approach

  1. Create a template function that takes two maps as input and returns a new map which is the intersection of the input maps.
  2. Inside this function, create an empty map that will store the intersection.
  3. Start by iterating the elements of the first map.
  4. For each key-value pair, check if the key exists in the second map.
  5. If the key exists in both maps, compare the value.
    1. If the value is same, add this pair to the intersection map.
  6. Keep doing that till all the elements of first map is scanned.

C++ Program to Find the Intersection of Two Maps

C++




// C++ Program to illustrate how to Find the Intersection of
// Two Maps
#include <iostream>
#include <map>
  
using namespace std;
  
// Template function to find the intersection of two maps
template <typename K, typename V>
map<K, V> find_intersection(const map<K, V>& map1,
                            const map<K, V>& map2)
{
    // Create an empty map to store the intersection
    map<K, V> intersection;
  
    // Iterate through each key-value pair in the first map
    for (const auto& pair : map1) {
        K key = pair.first;
        V value = pair.second;
  
        // Check if the key exists in the second map
        if (map2.count(key) > 0) {
            // If the key exists, add the key-value pair to
            // the intersection map
            intersection[key] = value;
        }
    }
  
    // Return the map containing the intersection elements
    return intersection;
}
  
int main()
{
    // Create two sample maps
    map<int, string> map1 = { { 1, "apple" },
                              { 2, "banana" },
                              { 3, "cherry" } };
    map<int, string> map2 = { { 2, "banana" },
                              { 4, "grape" },
                              { 3, "orange" } };
  
    // Find the intersection of the maps
    map<int, string> intersection
        = find_intersection(map1, map2);
  
    // Print the key-value pairs in the intersection map
    for (const auto& pair : intersection) {
        cout << pair.first << ": " << pair.second << endl;
    }
  
    return 0;
}


Output

2: banana
3: cherry

Time Complexity: O(N), where N is the number of elements of the first map.
Auxiliary Space: O(K) where K is the number of elements in the intersection vector.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads