# How to make an efficient difference of sets function?

How to write a nice code that removes all elements from given set A, that occur also in set B? I would like to use DeleteCases[] for this purpose, but have no good idea how to make it efficient.

SetDifference[A_,B_]:=DeleteCases[A, ??? ]


Or to find out if theres a ready to use function already, as in Lists as Sets there is nothing like that mentioned.

• There is Complement but it could be not efficient enough if you know that, for example, A has no duplicates. – Kuba Oct 18 '13 at 10:48

There is an adequate function: Complement, e.g.:

Complement[{a, b, c, d, e}, {a, c, d}]

{b, e}


It works also with more sets, lists or with any heads, e.g.:

Complement[{a, b, c, d, e}, {a}, {c, e}, {d}]

{b}