Skip to main content
1 of 2
E. Chan-López
  • 31.3k
  • 3
  • 29
  • 50

Using DeleteCases and Union:

SetDifference[sets : {__ ? VectorQ}, opts___] := Module[{sdiff},
    sdiff = 
    DeleteCases[First @ sets, 
     Apply[Alternatives, Union @@ Rest[sets]]];
    Piecewise @ {
            {sdiff, SameQ[opts, Null]},
            {Union @ sdiff, SameQ[opts, "SortAndDistinct"]}
        }
   ];

   l1 = {b, e, d, a, b, c, d}; l2 = {b, c}; l3 = {a, f};

   SetDifference[{l1, l2, l3}]

   (*{e, d, d}*)

   SetDifference[{l1, l2, l3}, "SortAndDistinct"]

   (*{d, e}*)
E. Chan-López
  • 31.3k
  • 3
  • 29
  • 50