Using DeleteCases
and Union
:
SetDifference[setsSetDifference[sets__?VectorQ,
Optional[opts : {__Automatic ?| VectorQ}"SortAndDistinct", opts___]Automatic]] :=
Module[
{setarray, sdiff, sdiffsorted},
sdiffsetarray = {sets};
sdiff = DeleteCases[First @ setssetarray,
Apply[Alternatives, Union @@ Rest[sets]]];Rest[setarray]]
Piecewise ];
sdiffsorted = Union @ {sdiff;
Switch[
{sdiff, SameQ[opts opts, Null]}Automatic,
{Union @ sdiff, SameQ[opts"SortAndDistinct",
"SortAndDistinct"]} sdiffsorted
}]
];
l1 = {b, e, d, a, b, c, d}; l2 = {b, c}; l3 = {a, f};
SetDifference[SetDifference[l1, l2, l3]
(*{l1e, d, d}*)
SetDifference[l1, l2, l3}], "SortAndDistinct"]
(*{d, e}*)
SetDifference[{a, b, b, d, e}, {c}, {c, e}, {d}]
(*{a, b, b}*)
SetDifference[{l1a, l2b, l3b, d, e}, {c}, {c, e}, {d}, "SortAndDistinct"]
(*{da, eb}*)