Wolfram Computation Meets Knowledge

MultisetUnion

Contributed by: Robert B. Nachbar (Wolfram Solutions)

Perform the union operation on multisets.

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

gives a sorted list of all the elements that appear in any of the listi. The number of occurances of each distinct element is the maximum of the counts in each of the listi.

ResourceFunction["MultisetUnion"][list]

effectively sorts the elements of list.

Details and Options

If the listi are considered as multisets, MultisetUnion gives their union.
The listi must have the same head, but it need not be List.

Examples

Basic Examples

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

Scope

MultisetUnion works with any head, not just List :

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

Properties and Relations

Compare to Union:

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

Resource History