3

Consider the following:

list1={a,b,c,d};
list2={c,d,e,f};
list3={g,d,c,h};

Now I would like to get the symmetric difference. Hence, the result must be {a,b,e,f,g,h} (whether the result is sorted or not is irrelevant)

BTW: Using Complement[list1,list2,list3] returns {a,b}, since Complement only "gives the elements in e_all which are not in any of the e_i." (Mathematica Documentation Center)

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
John
  • 4,361
  • 1
  • 26
  • 41
  • 2
    What you ask for is not the symmetric difference. The symmetric difference of the three sets you posted is {a, b, c, d, e, f, g, h}... – Henrik Schumacher Apr 14 '18 at 21:16

3 Answers3

8

I think you are looking for this:

Complement[Join[list1, list2, list3], 
 Intersection[list1, list2, list3]]

or as suggested in the comments:

Complement[Union[list1, list2, list3], 
 Intersection[list1, list2, list3]]

(*
=> {a, b, e, f, g, h}
*)

Note, that the h is present here but not the d. I assume this is a typo in the question.

2
x = {a, b, c, d};
y = {c, d, e, f};
z = {g, d, c, h}

Nowadays we get the wanted result more easily with:

Flatten @ UniqueElements[{x, y, z}]

{a, b, e, f, g, h}

As Henrik commented the question title is misleading:

SymmetricDifference[x, y, z]

{a, b, c, d, e, f, g, h}

UniqueElements and SymmetricDifference came with V 13.1

eldo
  • 67,911
  • 5
  • 60
  • 168
1

Another way using Counts and Select:

x = {a, b, c, d};
y = {c, d, e, f};
z = {g, d, c, h};

Keys@Select[Counts[Catenate@{x, y, z}], # == 1 &]

({a, b, e, f, g, h})

E. Chan-López
  • 23,117
  • 3
  • 21
  • 44