Wolfram Research

Function Repository Resource:

MultisetInclusionQ

Source Notebook

Test whether or not one multiset is included in another multiset

Contributed by: Robert B. Nachbar (Wolfram Solutions)

ResourceFunction["MultisetInclusionQ"][list1,list2]

yields True if all the elements of list2 are contained in list1 and False otherwise.

ResourceFunction["MultisetInclusionQ"][assoc1,assoc2]

yields True if all the values of assoc2 are less than or equal to the corresponding values of assoc1 and False otherwise.

Details and Options

MultisetInclusionQ tests for non-strict subsets and yields True if the listi are equivalent.
MultisetInclusionQ explicitly accounts for the multiplicity of elements.
The listi must have the same head, but it need not be List.
The values of assoc1 and assoc2 must be counts, that is, non-negative integer values.

Examples

Basic Examples

Test if a multiset is a subset of another multiset:

In[1]:=
ResourceFunction["MultisetInclusionQ"][{1, 2, 3}, {3, 1}]
Out[1]=
In[2]:=
ResourceFunction["MultisetInclusionQ"][{1, 2, 3}, {1, 3, 1}]
Out[2]=

Multisets as an Association of counts:

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

Scope

MultisetInclusionQ works with any head, not just List:

In[5]:=
ResourceFunction["MultisetInclusionQ"][f[a, b, c], f[c, a]]
Out[5]=

Properties and Relations

Compare to SubsetQ:

In[6]:=
ResourceFunction["MultisetInclusionQ"][{1, 2, 3}, {1, 3, 1}]
Out[6]=
In[7]:=
SubsetQ[{1, 2, 3}, {1, 3, 1}]
Out[7]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information