How to Delete Elements from List1 appearing in List2?
Use
DeleteCases[list1, Alternatives @@ list2]
In new versions (M8.0+), DeleteCases
is optimized on patterns not involving blanks, so this will be fast also for large lists. For earlier versions, this will work:
Replace[list1, Dispatch[Thread[list2 -> Sequence[]]],{1}]
being 2-3 times slower, but still very fast.
You are perhaps searching for Complement
. Complement[list1, list2]
results in {a, b, d}
. The result is sorted though. If you are looking for an unsorted complement, DeleteCases[list1, Alternatives @@ list2]
should probably work. I think there are some discussions on unsorted complements out there at google.
Iff each list is internally free of duplicates you can use this very quick method:
DeleteDuplicates[#2 ~Join~ #] ~Drop~ Length[#2] &[list1, list2]