# Symmetric Difference

I was recently flipping through a real analysis textbook, and came across a notation that I had not seen before. First, the set difference of two sets $A$ and $B$ (which I had seen before) was defined as $A \setminus B = A \cap B^c.$ Here $B^c$ is the set compliment of $B$, or all elements not in $B$. You can equivalently write the set difference as $A \setminus B = \{x \, |\, x \in A\, \text{and}\, x \notin B\}$

Then a new set operation was defined as follows $A \triangle B = (A \setminus B) \cup (B \setminus A).$ This operation was not given a name, but I looked it up and discovered that $A \triangle B$ is called the “symmetric difference” of the sets $A$ and $B$. In words, it is the union of both set differences. An equivalent, but more intuitive, way to express the symmetric difference is $A \triangle B = (A \cup B) \setminus (A \cap B),$ which is the union minus the intersection.

This operation can be implemented in Mathematica (I got this implementation from http://mathworld.wolfram.com/SymmetricDifference.html). Define

SymmetricDifference[a_, b_] :=
Union[Complement[a, b], Complement[b, a]]

For example, if I define the sets

a = {1, 2, 3, 4, 5};
b = {4, 5, 6, 7, 8};

then SymmetricDifference[a,b] returns {1,2,3,6,7,8}.

The notation $A \triangle B$ does risk confusion with the Laplace operator on a function $f$: $\Delta f = \nabla \cdot \nabla f.$ In LaTeX, I used \triangle for the symmetric difference, and \Delta for the Laplacian. The MathWorld article linked above recommends using \ominus, so that the symmetric difference of sets $A$ and $B$ is given by $A \ominus B.$