multisetUnion<T> function

Map<T, int> multisetUnion<T>(
  1. Map<T, int> a,
  2. Map<T, int> b
)

Bag: count per element. Union = max counts, intersection = min, difference = a - b (floor 0).

Implementation

Map<T, int> multisetUnion<T>(Map<T, int> a, Map<T, int> b) {
  final Map<T, int> out = Map<T, int>.from(a);
  for (final MapEntry<T, int> e in b.entries) {
    final int cur = out[e.key] ?? 0;
    if (e.value > cur) out[e.key] = e.value;
  }
  return out;
}