Wolfram Research

Function Repository Resource:

MultisetSymmetricDifference

Source Notebook

Get the symmetric difference of the given lists

Contributed by: George Beck

ResourceFunction["MultisetSymmetricDifference"][L1,L2,]

gives the list of elements that occur an odd number of times in the lists L1,L2,.

Details and Options

For two lists with no duplicates, ResourceFunction["MultisetSymmetricDifferences"] gives the usual symmetric difference of the lists considered as sets, which is the Complement of the Union and Intersection.
The result is sorted.

Examples

Basic Examples

Here is the symmetric difference of two sets:

In[1]:=
ResourceFunction["MultisetSymmetricDifference"][{1, 2, 3}, {3, 4, 5}]
Out[1]=

Another example:

In[2]:=
ResourceFunction[
 "MultisetSymmetricDifference"][{1, 2, 3}, {2, 3, 4}, {3, 4, 5}]
Out[2]=

Like Union, Intersection and Complement, a single list is pruned of duplicates and sorted:

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

Possible Issues

Define two lists, one with a duplicated element:

In[4]:=
a = {1, 1, 2};
b = {1, 2};

The lists are equal up to duplicates, so the set symmetric difference is empty:

In[5]:=
Complement[Union[a, b], Intersection[a, b]]
Out[5]=

The element 1 occurs an odd number of times in the multiset sum of the lists:

In[6]:=
ResourceFunction[
ResourceObject[
Association[
   "Name" -> "MultisetSum", "ShortName" -> "MultisetSum", "UUID" -> "b2fb3550-2cfc-4a16-b563-2e4be9159a85", "ResourceType" -> "Function", "Version" -> "1.0.0", "Description" -> "Perform the sum operation on multisets", "RepositoryLocation" -> URL[
     "https://www.wolframcloud.com/objects/resourcesystem/api/1.0"], "SymbolName" -> "FunctionRepository`$\
d427729d684543cc9e93a3710cbc72fa`MultisetSum", "FunctionLocation" -> CloudObject[
     "https://www.wolframcloud.com/objects/dbf60646-0988-4542-89c3-\
fc5a1933d972"]], ResourceSystemBase -> "https://www.wolframcloud.com/objects/\
resourcesystem/api/1.0"]][a, b]
Out[6]=

Therefore 1 is in the multiset difference:

In[7]:=
ResourceFunction["MultisetSymmetricDifference"][a, b]
Out[7]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

See Also

License Information