0
$\begingroup$

I want to count the number of consecutive natural numbers for a list of natural numbers.

For example If {1,2,3,7,9,10,15} then Length @ {1,2,3} = 3 & Length @ {9,10} = 2 and so I should get {{1,2,3},{9,10}} or {3,2} as a result.

However, the result is not shown because the following command is used.

test = {1, 2, 3, 7, 9, 10, 15}; Gather [test, # 1 + 1 == # 2 &]

However, this result is as follows. {{1, 2}, {3}, {7}, {9, 10}, {15}}

I want to ask how to fix it. How to list ArrayFilter?

$\endgroup$
3
$\begingroup$
lst = {1, 2, 3, 7, 9, 10, 15}
SequenceCases[Differences@lst, {p : Repeated[1]} :> 1 + Length[{p}]]
$\endgroup$
2
$\begingroup$
split = Select[Split[test, #1 + 1 == #2 &], Length@# > 1 &]

{{1, 2, 3}, {9, 10}}

Length /@ split

{3, 2}

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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