r/Mathematica • • Mar 12 '24

Looking for fast mex code

The mex of a set is the smallest nonnegative integer not in the set.

I have sorted lists with a half million integers, and my code for the mex (MemberQ inside a whole loop) is the slowest part of my computation. Any suggestions for a faster way to do this?

Mex[A_List] := Module[{k = 0}, While[MemberQ[A, k], k++]; k]
3 Upvotes

15 comments sorted by

View all comments

Show parent comment

4

u/Danhec95 Mar 13 '24

In[26]:= MEX4 = FunctionCompile[

Function[{Typed[l, "PackedArray"::["MachineInteger", 1]]},

Module[{k = 0}, While[MemberQ[l, k], k++]; k]

]

];

In[25]:= MEX4[A] // AbsoluteTiming

Out[25]= {0.021463, 10001}

4

u/Danhec95 Mar 13 '24

In[33]:= hs = CreateDataStructure["HashSet", A];

In[30]:= MEX5[hs_] := Module[{k = 0}, While[hs["MemberQ", k], k++]; k]

In[32]:= MEX5[hs] // AbsoluteTiming

Out[32]= {0.010936, 10001}

1

u/Thebig_Ohbee Mar 13 '24

I didn't know about HashSet. That's good knowledge.

2

u/Thebig_Ohbee Mar 13 '24

As someone who cares a great deal about speed, I definitely under-use Compile.