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

ResourceFunction["MultisetInclusionQ"] tests for non-strict subsets and yields True if the listi are equivalent.
ResourceFunction["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 (2) 

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 (1) 

MultisetInclusionQ works with any head, not just List:

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

Properties and Relations (1) 

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

Publisher

Robert Nachbar

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 19 February 2019

Related Resources

License Information