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

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

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

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 both empty, the answer is True:

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

Neat Examples (1) 

What are the chances that two randomly generated lists of length 10 overlap exactly? Use ContainsAllMultiplicities in conjunction with the resource function Proportions to find out:

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

Publisher

SHuisman

Version History

  • 1.0.0 – 25 September 2019

License Information