How to Find the Difference of Two Maps in C++? Last Updated : 06 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++, a map is a container that stores key-value pairs in an ordered or sorted manner. Finding the difference between two maps involves determining the elements that are present in one map but not in the other. In this article, we will learn how to find the difference between two Maps in C++. Example : Input:myMap1 = { {1: "C++"}, {2: "Java"}, {3: "Python"} }myMap2 = { {2: "Java"}, {3: "C"}, {4: "JavaScript"} }Output :Difference between map1 and map2:Key: 1, Value: C++Key: 3, Value: PythonKey: 4, Value: JavaScriptFind the Difference of Two Maps in C++To find the difference between the two maps, we will start checking in both of the maps, where the difference is. We can do this by simply iterating through both of the maps. ApproachWe will iterate through the first map to check which element is different from the second map by comparing the key and values of two elements of both maps.We will push the different elements to another map container that will store the difference of two maps.In the second iteration, we will check in the second map for the elements that are different from the first map.We will then push the different elements to the map that contains the different elements form the first map. C++ Program to Find the Difference of Two Maps C++ // C++ Program to illustrate how to find the difference of // two maps #include <iostream> #include <map> using namespace std; int main() { // Initialize two maps map<int, string> map1 = { { 1, "C++" }, { 2, "Java" }, { 3, "Python" } }; map<int, string> map2 = { { 2, "Java" }, { 3, "Python" }, { 4, "JavaScript" } }; // Create a map to store the difference map<int, string> diff; // Find the difference of the two maps for (auto& pair : map1) { if (map2.find(pair.first) == map2.end()) { diff.insert(pair); } } // Print the difference of the two maps for (auto& pair : diff) { cout << "{" << pair.first << ", " << pair.second << "}, "; } cout << endl; return 0; } Output{1, C++}, Time Complexity : O(N logM, M logN), where N and M are the sizes of the input maps map1 and map2, respectively.Auxiliary Space: O(N + M) Comment More infoAdvertise with us Next Article How to Find the Difference of Two Maps in C++? S subhra_ghosh Follow Improve Article Tags : C++ Programs C++ STL cpp-map CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How To Find the Difference of Two Multimaps in C++? In C++ STL, finding the difference between two multimaps consists of identifying the elements that exist in one multimap but are not present in the other. In this article, we will learn how to find the difference between two multimaps in C++ STL. Example:Input: multimap1 = {1, "Java"}, {2, "Python"} 2 min read How to Find Symmetric Difference of Two Maps in C++? In C++, a map is a container that stores elements in a mapped fashion. Each element has a key value and a mapped value. The symmetric difference between two sets is formed by the elements that are present in one of the sets, but not in the other. In this article, we will learn how to find the symmet 2 min read How to Find the Difference of Two Sets in C++? In C++, the set container provides an efficient way to store unique elements in sorted order. Finding the difference between two sets involves determining the elements that are present in one set but not in the other. In this article, we are going to learn how to find the difference of two sets in C 2 min read How to Find the Difference of Two Deques in C++? In C++, deques containers are sequential containers that allow the insertion and deletion of elements at both the beginning and end. In this article, we will learn how to find the difference between two deques in C++. Example: Input: deque1 = {1, 2, 3, 4, 5}; deque2 = {3, 4, 5, 6, 7}; Output: deque1 2 min read How to Find Symmetric Difference of Two Multimaps in C++? In C++, a multimap is a container that contains a sorted list of key-value pairs and there can be multiple entries having the same key. The symmetric difference between two multimaps gives all the unique elements from both multimaps. In this article, we will see how to find the symmetric difference 2 min read Like