Wolfram Research

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 and Options

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

Examples

Basic Examples

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

MultisetCardinality works with any head, not just List:

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

Properties and Relations

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]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information