# Ordered Subsequences of consecutive integers

I have a list

  list={3, 4, 8, 1, 2, 5, 6, 7, 9}


and I want to find all the ordered subsequences of consecutive integers

in my example I should get

 {{3, 4}, {1, 2}, {5, 6, 7}}


as you can see {5,6}, {6,7} must NOT be included in the final result

• closely related: 23607
– Kuba
Mar 19, 2017 at 20:54

Didn't find an exact duplicate so let's use another closely related:

consecutiveQ = Most[#] == Rest[#] - 1 &

SequenceCases[{3, 4, 8, 1, 2, 5, 6, 7, 9}, {_, __}?consecutiveQ]

{{3, 4}, {1, 2}, {5, 6, 7}}

• thanx.it works! and it's elegant Mar 19, 2017 at 21:11
• @Jenny Elegant, but unfortunately extremely slow on long lists. Please see my answer for a comparison. (My apologies Kuba for "attacking" your method.) Jul 6, 2017 at 7:05
• @Mr.Wizard always feel free to do so as almost always I ignore performance in my answers.
– Kuba
Jul 6, 2017 at 7:12
Select[Split[list, #2 - #1 == 1 &], Length[#] > 1 &]


Also can get the same result

• Can you modify this so that if you have a list like {1,2,3,5,7,8,9} you will get back {{1,2,3},{5},{7,8,9}} instead of {{1,2,3},{7,8,9}} ???? I am tempted to post the question but I thought I would ask first.
– EGME
Oct 11, 2019 at 12:21
• Ok, I figured out how to do this, you just set Length[#]>=1& ...
– EGME
Oct 11, 2019 at 13:41

Based on intervals from Find subsequences of consecutive integers inside a list:

consec[a_List] :=
{a[[Prepend[# + 1, 1]]], a[[Append[#, -1]]]} & //
Range @@@ Pick[#\[Transpose], Unitize[Subtract @@ #], 1] &

consec[{3, 4, 8, 1, 2, 5, 6, 7, 9}]

{{3, 4}, {1, 2}, {5, 6, 7}}


Benchmark including the two existing answers as fnK and fnY:

consecutiveQ = Most[#] == Rest[#] - 1 &;

fnK[lst_] := SequenceCases[lst, {_, __}?consecutiveQ]
fnY[lst_] := Select[Split[lst, #2 - #1 == 1 &], Length[#] > 1 &]

Needs["GeneralUtilities"]

BenchmarkPlot[{fnK, fnY, consec}, RandomInteger[9, #] &, 2]
`

(Note the log-log scale.)