intersection method
Returns the intersection of this multiset with other.
The multiplicity of each element in the result is the minimum
of its multiplicities in this multiset and other.
Example:
final p = MultiSet<String>.fromIterable(['a', 'a', 'a', 'c']);
final q = MultiSet<String>.fromIterable(['a', 'a', 'b', 'c', 'c']);
final result = p.intersection(q);
// result = {'a': 2, 'c': 1}
Implementation
MultiSet<T> intersection(MultiSet<T> other) {
final result = MultiSet<T>();
final allElements = {..._elements.keys, ...other._elements.keys};
for (var element in allElements) {
final count = _min(
_elements[element] ?? 0,
other._elements[element] ?? 0,
);
if (count > 0) result.add(element, count);
}
return result;
}