Multiset Equivalence Problem

Unlike a set, a multiset may contain multiple occurrences of same number. The multiset equivalence problem states to check if two given multisets are equal or not. For example let A = {1, 2, 3} and B = {1, 1, 2, 3}. Here A is set but B is not (1 occurs twice in B), whereas A and B are both multisets. More formally, “Are the sets of pairs defined as equal for the two given multisets?”

Given two multisets A and B, write a program to check if the two multisets are equal.

Note: Elements in the multisets can be of order 109

Examples:

Input : A = {1, 1, 3, 4},
B = {1, 1, 3, 4}
Output : Yes

Input : A = {1, 3},
B = {1, 1}
Output : No

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Since the elements are as large as 10^9 we cannot use direct index table.

One solution is to sort both multisets and compare them one by one.

C++

 // C++ program to check if two given multisets // are equivalent #include using namespace std;    bool areSame(vector& a, vector& b) {     // sort the elements of both multisets     sort(a.begin(), a.end());     sort(b.begin(), b.end());        // Return true if both multisets are same.     return (a == b); }    int main() {     vector a({ 7, 7, 5 }), b({ 7, 5, 5 });     if (areSame(a, b))         cout << "Yes\n";     else         cout << "No\n";     return 0; }

Java

 // Java program to check if two given multisets  // are equivalent  import java.util.*; class GFG {       static boolean areSame(Vectora, Vectorb)  {      // sort the elements of both multisets      Collections.sort(a);      Collections.sort(b);         // Return true if both multisets are same.      return (a == b);  }   public static void main(String[] args) {        Vector a = new Vector(Arrays.asList( 7, 7, 5 ));        Vector b = new Vector(Arrays.asList( 7, 5, 5));     if (areSame(a, b))          System.out.print("Yes\n");      else         System.out.print("No\n");     } } // This code is contributed by PrinciRaj1992

Python3

 # Python3 program to check if  # two given multisets are equivalent    def areSame(a, b):            # sort the elements of both multisets     a.sort();     b.sort();        # Return true if both multisets are same.     return (a == b);    # Driver Code a = [ 7, 7, 5 ]; b = [ 7, 5, 5 ]; if (areSame(a, b)):     print("Yes"); else:     print("No");    # This code is contributed by Princi Singh

C#

 // C# program to check if two given multisets  // are equivalent  using System; using System.Collections.Generic;    class GFG {    static bool areSame(Lista, Listb)  {      // sort the elements of both multisets      a.Sort();     b.Sort();        // Return true if both multisets are same.      return (a == b);  }     // Driver code public static void Main() {     List a = new List { 7, 7, 5 };     List b = new List { 7, 5, 5 };     if (areSame(a, b))          Console.WriteLine("Yes\n");      else         Console.WriteLine("No\n"); } }    // This code is contributed by Rajput-Ji

Output:

No

A better solution is to use hashing. We create two empty hash tables (implemented using unordered_map in C++). We first insert all items of first multimap in first table and all items of second multiset in second table. Now we check if both hash tables contain same items and frequencies or not.

 // C++ program to check if two given multisets // are equivalent #include using namespace std;    bool areSame(vector& a, vector& b) {     if (a.size() != b.size())         return false;        // Create two unordered maps m1 and m2     // and insert values of both vectors.     unordered_map m1, m2;     for (int i = 0; i < a.size(); i++) {         m1[a[i]]++;         m2[b[i]]++;     }        // Now we check if both unordered_maps     // are same of not.     for (auto x : m1) {         if (m2.find(x.first) == m2.end() ||              m2[x.first] != x.second)             return false;     }        return true; }    // Driver code int main() {     vector a({ 7, 7, 5 }), b({ 7, 7, 5 });     if (areSame(a, b))         cout << "Yes\n";     else         cout << "No\n";     return 0; }

Output:

Yes

Time complexity : O(n) under the assumption that unordered_map find() and insert() operations work in O(1) time.

My Personal Notes arrow_drop_up I am undergrad from MN National Institute of Technology Allahabad Design is my passion and algorithms are my food for thought

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.