Let L be a list of integers, and ⓝ denote an explicit integer.
I've thought the most natural way to tell whether ⓝ belongs to L is
In[1] MemberQ[L, ⓝ]
But there are problems.
1>> If Length[L] is too big(around 10^9), then the code is indeed harmful. Mathematica automatically quits krenel(lose every definition).
2>> Try
In[2] MemberQ[Range[10^8],1]//Timing
Out[2] (* Takes about 3 seconds, too long *)
If we know ⓝ appears very early in L, then using MemberQ may be inefficient.
Now AnyTrue technique is the following :
In[3] AnyTrue[L, #==ⓝ&]
Introducing advantages :
- The code works well even in case
Length[L]is too big. - If ⓝ appears very early in L, then it takes very short time. (more precisely, the earlier, the shorter)
- Its usage is much more diverse than
MemberQ.
So I'll never use MemberQ if Length[L] is too big or if I know ⓝ appears early in L.
But I found an advantages of MemberQ, it behaved faster for irregular data.
L=RandomSample[Range[10^8]];
MemberQ[L, ⓝ]//Timing
AnyTrue[L, #==ⓝ&]//Timing
There can be other better method. Can you tell me more about these kind of things ? I mean performance of various test(that can tell the existence of something in the list).

MemberQreally not shortcutting? – Alan Aug 29 '21 at 19:00IntersectingQ[L,{n}]//Timing– Bill Aug 29 '21 at 19:10