MultiSet<T> class

Implements a MultiSet (Bag) with union, intersection, difference, and count operations.

A MultiSet allows duplicate elements. This implementation uses a map to track element counts.

  • add(value, [count]): Adds value to the multiset count times (default 1).
  • remove(value, [count]): Removes value from the multiset count times (default 1).
  • count(value): Returns the count of value in the multiset.
  • union(other): Returns a new MultiSet that is the union of this and other.
  • intersection(other): Returns a new MultiSet that is the intersection of this and other.
  • difference(other): Returns a new MultiSet that is the difference of this and other.

Time Complexity: O(n) for most operations, where n is the number of unique elements. Space Complexity: O(n)

Example:

final ms1 = MultiSet<int>([1, 2, 2, 3]);
final ms2 = MultiSet<int>([2, 3, 3, 4]);
print(ms1.union(ms2)); // Outputs: {1: 1, 2: 3, 3: 3, 4: 1}
print(ms1.intersection(ms2)); // Outputs: {2: 1, 3: 1}
print(ms1.difference(ms2)); // Outputs: {1: 1}

Constructors

MultiSet([Iterable<T>? items])

Properties

hashCode int
The hash code for this object.
no setterinherited
length int
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

add(T value, [int count = 1]) → void
count(T value) int
difference(MultiSet<T> other) MultiSet<T>
intersection(MultiSet<T> other) MultiSet<T>
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
remove(T value, [int count = 1]) bool
toSet() Set<T>
toString() String
A string representation of this object.
override
union(MultiSet<T> other) MultiSet<T>

Operators

operator ==(Object other) bool
The equality operator.
inherited