Function Repository Resource:

MultisetUnion

Source Notebook

Perform the union operation on multisets

Contributed by: Robert B. Nachbar (Wolfram Solutions)

ResourceFunction["MultisetUnion"][list1,list2,]

gives a sorted list of all the elements that appear in any of the listi.

ResourceFunction["MultisetUnion"][list]

effectively sorts the elements of list.

Details and Options

If the listi are considered multisets, ResourceFunction["MultisetUnion"] gives their union.
When forming the union, the number of occurences of each distinct element is the maximum of the counts of that element in each of the listi.
The listi must have the same head, but it need not be List.

Examples

Basic Examples (2) 

Give a sorted list of elements from all the lists:

In[1]:=
ResourceFunction[
 "MultisetUnion"][{a, b, a, c}, {d, a, e, b}, {c, d, a, d}]
Out[1]=

Multisets as an Association of counts:

In[2]:=
ResourceFunction[
 "MultisetUnion"][<|"a" -> 3, "b" -> 1, "c" -> 2, "d" -> 1|>,
 <|"a" -> 2, "b" -> 2, "c" -> 2, "e" -> 1|>,
 <|"a" -> 1, "b" -> 2, "c" -> 2, "e" -> 2|>]
Out[2]=

Scope (1) 

MultisetUnion works with any head, not just List:

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

Properties and Relations (1) 

Compare to Union:

In[4]:=
ResourceFunction[
 "MultisetUnion"][{a, b, a, c}, {d, a, e, b}, {c, d, a, d}]
Out[4]=
In[5]:=
Union[{a, b, a, c}, {d, a, e, b}, {c, d, a, d}]
Out[5]=

Publisher

Robert Nachbar

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 2.0.0 – 04 March 2019
  • 1.0.0 – 07 February 2019

Related Resources

License Information