Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need to create an array subtracting two arrays from another array. I know how to subtract one:

am = DeleteCases[amp, Alternatives @@ af]

I can subtract an array each time but there must be a way to subtract more than one array at once. So I want to subtract af and ai from amp. How can I do that at once?

share|improve this question
up vote 1 down vote accepted

You can use Select as well :

ar = Range[10];
ar1 = Range[1, 10, 2];
ar2 = Range[1, 10, 3];

Select[ar, Not[MemberQ[Union[ar1, ar2], #]] &]
(* {2, 6, 8} *)

DeleteCases[ar, _?(MemberQ[Union[ar1, ar2], #] &)]
(* {2, 6, 8} *)
share|improve this answer
Or, DeleteCases[ar, Alternatives @@ Union[ar1, ar2]]. – VLC Feb 25 '13 at 10:26
Not@MemberQ is equivalent to FreeQ. – chyaong Mar 11 '13 at 11:18

The built-in function Complement does exactly what you need:

enter image description here


ar = Range[10];
ar1 = Range[1, 10, 2];
ar2 = Range[1, 10, 3];

elements in the first list that are not in any of the subsequent lists are:

Complement[ar, ar1,ar2]
(* {2, 6, 8 *)
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.