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