Is There Something Like Mulitiset In Javascript?
I know that JavaScript now has sets, but I wonder if there is something to realize the function of multiSet, or if there is some framework that has the functions of multiset which
Solution 1:
There are no built-in multiset structure, but there are some libraries that have such:
Feel free to add your favorite library in this question.
Solution 2:
You can build your own multiset using the Builtin map type and numerical values:
classMultiset {
_backing = newMap();
add(value) {
if (this._backing.has(value)) {
this._backing.set(value, 1 + this._backing.get(value));
} else {
this._backing.set(value, 1);
}
}
delete(value) {
if (this._backing.get(value) > 0) {
this._backing.set(value, this._backing.get(value) - 1);
} else {
//do nothing
}
}
get(value) {
if (this._backing.get(value) > 0) {
returnthis._backing.get(value);
} else {
return0;
}
}
}
Post a Comment for "Is There Something Like Mulitiset In Javascript?"