difference method

MultiSet<T> difference(
  1. MultiSet<T> other
)

Returns the difference of this multiset with other.

The multiplicity of each element in the result is the multiplicity in this multiset minus the multiplicity in other (minimum 0).

Example:

final p = MultiSet<String>.fromIterable(['a', 'a', 'a', 'b']);
final q = MultiSet<String>.fromIterable(['a', 'a', 'b', 'b']);
final result = p.difference(q);
// result = {'a': 1}

Implementation

MultiSet<T> difference(MultiSet<T> other) {
  final result = MultiSet<T>();

  for (var element in _elements.keys) {
    final count = (_elements[element] ?? 0) - (other._elements[element] ?? 0);
    if (count > 0) result.add(element, count);
  }

  return result;
}