Wolfram Research

Function Repository Resource:

ContainsAllMultiplicities

Source Notebook

Similar to ContainsAll but taking the multiplicities into consideration

Contributed by: Sander Huisman

ResourceFunction["ContainsAllMultiplicities"][e1,e2]

yields True if e1 contains all of the elements of e2 taking into account multiplicities.

ResourceFunction["ContainsAllMultiplicities"][e2]

is an operator form that yields True when the object to which it is applied contains all of the elements of e2.

Examples

Basic Examples

Check if the elements of the second list are all contained in the first list:

In[1]:=
ResourceFunction["ContainsAllMultiplicities"][{b, a, b, c}, {a, b, b}]
Out[1]=

Check if the elements of the second list are all contained in the first list:

In[2]:=
ResourceFunction[
 "ContainsAllMultiplicities"][{b, a, b, c}, {a, b, b, b}]
Out[2]=

Scope

Make a function that checks if a list has one 1, two 2s and one 3:

In[3]:=
operator = ContainsAll[{1, 2, 2, 3}]
Out[3]=

Apply to a bunch of lists:

In[4]:=
Tally[operator /@ RandomInteger[10, {1000, 5}]]
Out[4]=

Properties and Relations

Elements of e1 and e2 can be lists themselves:

In[5]:=
ResourceFunction[
 "ContainsAllMultiplicities"][{a, b, c, {1, 1}}, {{1, 1}, c, b}]
Out[5]=

There are not enough c’s in the first list:

In[6]:=
ResourceFunction[
 "ContainsAllMultiplicities"][{a, b, c, c, c, c}, {c, a, c, c, c, c, b}]
Out[6]=

Check the same but without considering the multiplicities:

In[7]:=
ContainsAll[{a, b, c, c, c, c}, {c, a, c, c, c, c, b}]
Out[7]=

If e1 is empty and e2 is non-empty, the answer is always False:

In[8]:=
ResourceFunction["ContainsAllMultiplicities"][{}, {a, b, b, c}]
Out[8]=

If e2 is empty, the answer is always True:

In[9]:=
ResourceFunction["ContainsAllMultiplicities"][{a, b, c}, {}]
Out[9]=

If e1 and e2 are empty, the answer is True:

In[10]:=
ResourceFunction["ContainsAllMultiplicities"][{}, {}]
Out[10]=

Neat Examples

What are the chances that two randomly generated lists of length 10 overlap exactly:

In[11]:=
results = Table[ResourceFunction["ContainsAllMultiplicities"][
    RandomInteger[3, 10], RandomInteger[3, 10]], {10000}];
ResourceFunction["Proportions"][results] // N
Out[12]=

Resource History

License Information