Function Repository Resource:

UnsortedMultisetComplement

Source Notebook

Perform the complement operation on multisets without sorting

Contributed by: Peter Cullen Burbery

ResourceFunction["UnsortedMultisetComplement"][listall,list1,list2,]

gives the elements in listall that are in excess of those in all the listi maintaining the original order.

Details

The list returned by ResourceFunction["UnsortedMultisetComplement"] is not sorted into standard order. The list returned by the resource function MultisetComplement is sorted into standard order.
If the listi are considered as multisets, ResourceFunction["UnsortedMultisetComplement"] gives their complement.
DeleteDuplicates is to Union and UnsortedComplement is to Complement as ResourceFunction["UnsortedMultisetComplement"] is to MultisetComplement.

Examples

Basic Examples (3) 

Find the complement of a multiset with another multiset, maintaining the original order:

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

Find a complement of a multiset to two other multisets. First generate the multisets:

In[2]:=
list = RandomSample[
  Catenate[
   MapThread[ConstantArray, {{p, q, r, w}, {1, 2, 3, 4} + 5}]]]
Out[2]=
In[3]:=
list1 = Catenate[MapThread[ConstantArray, {{p, q, r, w}, Range[4]}]]
Out[3]=
In[4]:=
list2 = Catenate[
  MapThread[ConstantArray, {{p, q, r, w}, Reverse[Range[4]]}]]
Out[4]=

Find the complement:

In[5]:=
ResourceFunction["UnsortedMultisetComplement"][list, list1, list2]
Out[5]=

Find letters in one word not present in another, maintaining the order:

In[6]:=
ResourceFunction["UnsortedMultisetComplement"][
 Characters@"ballistics", Characters@"basic"]
Out[6]=

Properties and Relations (2) 

Compare with the resource function UnsortedComplement:

In[7]:=
ResourceFunction["UnsortedComplement"][
 List[q, r, r, r, p, q, q, r, r, s, p, p, p], List[q, p, r], List[p, r, p, q]]
Out[7]=
In[8]:=
ResourceFunction["UnsortedMultisetComplement"][
 List[q, r, r, r, p, q, q, r, r, s, p, p, p], List[q, p, r], List[p, r, p, q]]
Out[8]=

Here is a step-by-step demonstration of what UnsortedMultisetComplement does:

In[9]:=
RandomSample[
 Catenate[MapThread[ConstantArray, {{p, q, r, w}, {1, 2, 3, 4} + 2}]]]
Out[9]=

We will be removing 1 p, 2 qs, 3 rs, and 4ws, starting from the back.

Remove 1 p:

In[10]:=
{w, w, p, p, r, w, q, w, r, q, r, q, r, w, w, q, r};

Remove 2 qs:

In[11]:=
{w, w, p, p, r, w, q, w, r, q, r, r, w, w, r};

Remove 3 rs:

In[12]:=
{w, w, p, p, r, w, q, w, r, q, w, w};

Remove 4 ws:

In[13]:=
{w, w, p, p, r, q, r, q};
In[14]:=
ResourceFunction[
 "UnsortedMultisetComplement"][{w, w, p, p, r, w, p, q, w, r, q, r, q,
   r, w, w, q, r}, {p, q, q, r, r, r, w, w, w, w}]
Out[14]=

This is what we have:

In[15]:=
{w, w, p, p, r, q, r, q} === ResourceFunction[
  "UnsortedMultisetComplement"][{w, w, p, p, r, w, p, q, w, r, q, r, q, r, w, w, q, r}, {p, q, q, r, r, r, w, w, w, w}]
Out[15]=

Neat Examples (1) 

Remove characters of a short word from a long word to form a new word:

In[16]:=
StringJoin@
 ResourceFunction["UnsortedMultisetComplement"][
  Characters["sediment"], Characters["dime"]]
Out[16]=

Publisher

Peter Burbery

Requirements

Wolfram Language 13.1 (June 2022) or above

Version History

  • 1.0.0 – 18 August 2023

Related Resources

License Information