0
$\begingroup$

I was wondering if there was a more elegant way of finding whether an element exists in an N-dimensional array. The way I would do it is:

 inArrayQ[a_,el_] := If[Length@Position[a,x_/;x==el]==0,False,True];

But this seems rather cumbersome. I am surprised there is no built-in Mathematica function that does this, but if not, is there a better way to write such a function?

$\endgroup$
4
  • 3
    $\begingroup$ check out MemberQ $\endgroup$ Sep 20, 2013 at 20:07
  • 1
    $\begingroup$ D'oh, if only the documentation search was better...thanks. $\endgroup$
    – Guillochon
    Sep 20, 2013 at 20:08
  • 1
    $\begingroup$ @Guillochon If you search for MemberQ you get the answer right away ;) $\endgroup$ Sep 20, 2013 at 20:31
  • 1
    $\begingroup$ @Guillochon You can also use Position to determine where the element is. $\endgroup$
    – RM1618
    Sep 22, 2013 at 10:40

1 Answer 1

2
$\begingroup$

Not to have any more open, unanswered questions

Check out MemberQ, you should find all information there.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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