Multiset implementation
const Multiset = require('@emilbayes/multiset')
const m = new Multiset([1, 1, 2, 3, 4, 4, 3, 8, 1])
console.log(m.count(1)) // => 3
console.log(m.count(4)) // => 2
console.log(m.size) // => 9
console.log(m.uniqueSize) // => 5
m.add(10)
console.log(m.size) // => 10
console.log(m.uniqueSize) // => 6Create a new Multiset with optional initial array of elements, and optional
identity key, which defaults to strict equality.
Number of total elements in the Multiset.
Number of unique elements in the Multiset.
Add elm to the Multiset, with optional multiplicity n.
Equality is checked with key.
Delete elm from the Multiset, optionally decrementing by n.
If final count is 0 or less, the element is removed from the Multiset.
Equality is checked with key.
Check for existence.
Count occurrences of elm in the Multiset.
Check if m and rhs contain the same elements with same multiplicity.
Check if m is a subset of superset, meaning superset contain the same
elements with at least the least the same multiplicity as m.
Filter unique elements from the Multiset, preserving count in the new Multiset. A new Multiset is created but if the elements are not primitives, they will share references between the two Multisets.
Find the difference m \ rhs, copying the Multiset with the difference
contained in the new Multiset. rhs can either be a Multiset or an Array.
Iterate elements of the Multiset as an array, meaning elements with multiplicity
will be enumerated n times.
Map elements of the Multiset as an array, meaning elements with multiplicity
will be enumerated n times. Returns the resulting array.
Reduce elements of the Multiset as an array, meaning elements with multiplicity
will be enumerated n times. Returns the resulting accumulated value.
Clone the Multiset. A new Multiset is created but if the elements are not primitives, they will share references between the two Multisets.
Iterate elements of the Multiset with multiplicity.
Iterate [elm, multiplicity] of the Multiset.
Iterate multiplicity of the Multiset.
Iterate unqiue elm of the Multiset.
npm install @emilbayes/multiset