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]): Addsvalueto the multiset count times (default 1).remove(value, [count]): Removesvaluefrom the multiset count times (default 1).count(value): Returns the count ofvaluein the multiset.union(other): Returns a new MultiSet that is the union of this andother.intersection(other): Returns a new MultiSet that is the intersection of this andother.difference(other): Returns a new MultiSet that is the difference of this andother.
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}
Properties
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