Function Repository Resource:

MultisetCardinality

Source Notebook

Determine the cardinality for a multiset

Contributed by: Robert B. Nachbar (Wolfram Solutions)

ResourceFunction["MultisetCardinality"][list]

gives the cardinality of the multiset list.

ResourceFunction["MultisetCardinality"][assoc]

gives the cardinality of the Association multiset assoc, effectively giving the total of the values.

Details

The cardinality of a set is the number of elements it contains, explicitly accounting for the multiplicity of the elements.
The list need not have the head List.
The values of assoc must be counts—that is, non-negative Integer values.

Examples

Basic Examples (1) 

Give the cardinality:

In[1]:=
ResourceFunction["MultisetCardinality"][{a, b, a, c}]
Out[1]=
In[2]:=
ResourceFunction[
 "MultisetCardinality"][<|"a" -> 2, "b" -> 2, "c" -> 2, "d" -> 0, "e" -> 1|>]
Out[2]=

Scope (1) 

MultisetCardinality works with any head, not just List:

In[3]:=
ResourceFunction["MultisetCardinality"][f[b, b, a]]
Out[3]=

Properties and Relations (2) 

Compare to Length:

In[4]:=
ResourceFunction["MultisetCardinality"][{a, b, a, c}]
Out[4]=
In[5]:=
Length[{a, b, a, c}]
Out[5]=

Compare to Total:

In[6]:=
ResourceFunction[
 "MultisetCardinality"][<|"a" -> 2, "c" -> 2, "d" -> 0|>]
Out[6]=
In[7]:=
Total[<|"a" -> 2, "c" -> 2, "d" -> 0|>]
Out[7]=

Publisher

Robert Nachbar

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.1 – 11 August 2023
  • 1.0.0 – 18 February 2019

Related Resources

License Information