How To Find the Difference of Two Multimaps in C++? Last Updated : 01 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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"}, {3, "C++"}, {4, "JavaScript"} mutlimap2 = {2, "Python"}, {4, "JavaScript"}, {5, "TypeScript"} Output: Multimap after difference: 1: Java 3: C++Finding the Difference Between Two Multimaps in C++To find the difference between two std::multimap in C++, we can use the std::multimap::equal_range and follow the given approach. Approach: Iterate through the elements of the first multimap (multimap1) For each element in multimap1, check if the corresponding key exists in the second multimap (multimap2) using equal_range. If the key is not found, then insert the element into a new multimap (diff), Finally, print multimap diff which contains elements that are present in multi1 but not in multi2.C++ Program to Find the Difference of Two MultimapsThe below program demonstrates how we can find the difference between two multimaps in C++ STL. C++ // C++ Program to illustrate how to find the difference of // two multimaps #include <iostream> #include <map> using namespace std; int main() { // Defining multimap1 multimap<int, string> multi1 = { { 1, "Java" }, { 2, "Python" }, { 3, "C++" }, { 4, "JavaScript" } }; // Defining multimap2 multimap<int, string> multi2 = { { 2, "Python" }, { 4, "JavaScript" }, { 5, "TypeScript" } }; // Finding difference between multimap1 and multimap2 multimap<int, string> diff; for (const auto& pair : multi1) { auto range = multi2.equal_range(pair.first); if (range.first == range.second) { diff.insert(pair); } } // Printing difference cout << "Difference MultiMap:" << endl; for (const auto& pair : diff) { cout << pair.first << ": " << pair.second << endl; } return 0; } OutputDifference MultiMap: 1: Java 3: C++ Time Complexity: O(N * log M) where N is the number of elements in multi1 and M is the number of elements in multi2.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Find Symmetric Difference of Two Maps in C++? A anjalibo6rb0 Follow Improve Article Tags : C++ Programs C++ STL cpp-multimap CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Find the Difference of Two Maps in C++? 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++. Exam 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 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 Union of Two Multimaps in C++? In C++, finding the union of two multimaps consists of combining the elements from both multimap collections while considering the duplicates as a multimap allows multiple values to have the same key. In this article, we will learn to find the union of two multimaps in C++. Example:Input: multi1 = { 2 min read How To Find the Intersection of Two Multimaps in C++? In C++, finding the intersection of two multimaps consists of identifying the common elements that are shared between both collections, containing key-value pairs. In this article, we will learn how to find the intersection of two multimaps in C++ STL. Example:Input:multimap1 = {1, "Java"}, {2, "Pyt 2 min read Like