Symmetric difference

APL (Dyalog Unicode), 3 bytes

∪~∩

Try it online!

How it works

∪~∩  ⍝ Input: two sets as vectors
∪    ⍝ Set union
 ~   ⍝ Set minus
  ∩  ⍝ Set intersection

The rest is just for fun.

APL (Dyalog Unicode), 4 bytes

~∪~⍨

Try it online!

~∪~⍨
~     ⍝ Set difference (a~b)
 ∪    ⍝ Set union
  ~⍨  ⍝ Set difference reversed (b~a)

APL (Dyalog Unicode), 5 bytes

~⍨∪⍨~

Try it online!

This one is palindromic!

How it works

~⍨∪⍨~
~⍨     ⍝ Set difference reversed (b~a)
  ∪⍨   ⍝ Set union reversed
    ~  ⍝ Set difference (a~b)

Python 3, 11 bytes

set.__xor__

Try it online!

For built-in set objects, a^b computes symmetrical set difference. __xor__ is the magic name for that operator, and it is shorter than lambda a,b:a^b.

Also works in Python 2.


J, 8 6 bytes

-2 bytes thanks to Bubbler!

-.,-.~

Try it online!

K (oK), 11 bytes

{(x^y),y^x}

Try it online!

Tags:

Code Golf