ClearAll[f1, f2]
f1 = Module[{i = 1}, While[SequencePosition[#, Range[i], 1] =!= {}, i++]; i-1]&;
f2 = Module[{i = 0}, While[SequencePosition[#, Range[++i], 1] =!= {}]; i-1]&;
f1 /@ {{1, 2, 3, 5, 7}, {3, 2, 1, 5, 7}, {3, 2, 7, 5}}
{3, 1, 0}
f2 /@ {{1, 2, 3, 5, 7}, {3, 2, 1, 5, 7}, {3, 2, 7, 5}}
{3, 1, 0}
Timings:
Both f1 and f2 are faster than all the methods (except possibly fC in Fred Simon's answer which I cannot use because I don't have a C compiler installed) posted so far.
SeedRandom[42];
list = RandomInteger[{1, 10}, 10^6];
rsel = Max @ Select[Split[list,#1+1==#2&],#[[1]]==1&] // Timing; (* belisarius *)
rext = extend[list, Position[list, 1], 1] // Timing; (* xcah *)
rf = f[list] // Timing; (* fred simons *)
rf1 = f1[list] //Timing;
Grid[{{"Select", "extend", "f", "f1"}, {rsel, rext, rf, rf1}}, Dividers->All]// TeXForm
$\begin{array}{|c|c|c|c|c|}
\hline
\text{Select} & \text{extend} & f & \text{f1} & \text{f2} \\
\hline
\{1.453,5\} & \{0.137,5\} & \{0.039,5\} & \{0.008,5\} & \{0.008,5\} \\
\hline
\end{array}$
With list = RandomInteger[{1, 1000}, 10^6];
$\begin{array}{|c|c|c|c|c|}
\hline
\text{Select} & \text{extend} & f & \text{f1} & \text{f2} \\
\hline
\{1.68,2\} & \{0.047,2\} & \{0.039,2\} & \{0.012,2\} & \{0.011,2\} \\
\hline
\end{array}$
1. – Kuba Dec 12 '14 at 12:39Range[2]is not a subset of the given set. – rogerl Dec 12 '14 at 23:00