Multiset

A multiset is a generalization of a set that allows duplicate elements. Unlike normal sets, a multiset allows handling collections where elements can occur more than once.

Java example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import com.google.common.collect.Multiset;

Multiset<String> multiset = HashMultiset.create();

multiset.add("a");
multiset.add("b");
multiset.add("a");

multiset.count("a"); // 2 
multiset.count("b"); // 1

C++ example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

tree<string, null_type, less<string>, rb_tree_tag, tree_order_statistics_node_update> multiset;

multiset.insert("a"); 
multiset.insert("b");
multiset.insert("a");

multiset.count("a"); // 2
multiset.count("b"); // 1 

Python example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
from collections import Counter

multiset = Counter()

multiset["a"] += 1 
multiset["b"] += 1
multiset["a"] += 1 

multiset["a"] # 2
multiset["b"] # 1

Multisets provide counts for each element like a bag/histogram and are useful when duplicate elements need to be considered.

A multiset is a generalization of a set that, unlike a normal set, allows for multiple instances of the same element. The number of occurrences of each element is counted, and duplicate elements are allowed.

Some key properties of multisets:

  • Elements can occur more than once
  • Order does not matter
  • Size of a multiset is the total number of elements including duplicates
  • Two multisets are equal if they contain the same elements with the same frequencies

Multisets are useful for many problems where keeping track of frequencies is important, such as counting word occurrences in text.

Example in Java:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
import java.util.HashMap;

class Multiset {
  
  HashMap<Integer,Integer> map;

  public Multiset() {
    map = new HashMap<>();
  }

  public void add(int x) {
    map.put(x, map.getOrDefault(x, 0) + 1); 
  }

  public int count(int x) {
    return map.getOrDefault(x, 0);
  }

}

This implements a multiset by using a HashMap to store element frequencies.

Examples in C++ and Python follow a similar approach - using a hash table/dictionary to maintain counts of elements. The count method returns the frequency of a given element.

A Multiset, also known as a Bag, is a collection that allows multiple occurrences of elements. Unlike a set, where an element can appear at most once, a Multiset can contain duplicates. The key takeaway is that Multisets are useful when you need a collection that counts element occurrences and doesn’t enforce uniqueness.

Java Code for Multiset

Java doesn’t have a built-in Multiset class, but you can achieve similar functionality using a HashMap.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
import java.util.HashMap;

public class Multiset<T> {
    private HashMap<T, Integer> map;

    public Multiset() {
        map = new HashMap<>();
    }

    public void add(T element) {
        map.put(element, map.getOrDefault(element, 0) + 1);
    }

    public int count(T element) {
        return map.getOrDefault(element, 0);
    }
}
  1. add(T element) adds an element to the Multiset and updates its count.
  2. count(T element) returns the count of a given element in the Multiset.

C++ Code for Multiset

C++ provides a built-in multiset template class in the Standard Template Library (STL).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#include <set>
#include <iostream>

int main() {
    std::multiset<int> mset;
    mset.insert(1);
    mset.insert(1);
    mset.insert(2);

    int count = mset.count(1); // count will be 2
    std::cout << "Count of 1: " << count << std::endl;

    return 0;
}
  1. insert(int element) adds an element to the Multiset.
  2. count(int element) returns the number of occurrences of an element.

Python Code for Multiset

Python provides a Counter class in the collections module that can serve as a Multiset.

1
2
3
4
5
6
7
8
from collections import Counter

mset = Counter()
mset['a'] += 1
mset['a'] += 1
mset['b'] += 1

count = mset['a']  # count will be 2
  1. mset['a'] += 1 adds an element to the Multiset and increments its count.
  2. mset['a'] returns the count of the element ‘a’ in the Multiset.

Each of these implementations has methods for adding elements and checking their counts in the Multiset, showcasing how to maintain and query a collection of items where duplicates are allowed.