Skip to content

Instantly share code, notes, and snippets.

@DavidBruant
Created July 21, 2015 17:27
Show Gist options
  • Save DavidBruant/77ad4c4418ac8619c5d9 to your computer and use it in GitHub Desktop.
Save DavidBruant/77ad4c4418ac8619c5d9 to your computer and use it in GitHub Desktop.
"use strict";
var res = [4,5,6,8,9,10,11,12,15,17]
.reduce(function(acc, el, i){
if(i === 0)
return [[el]];
var lastArray = acc[acc.length - 1];
var lastEl = lastArray[lastArray.length - 1];
if(lastEl + 1 === el)
lastArray.push(el); // append to current sequence
else
acc.push([el]) // create new sequence
return acc;
}, [])
.reduce(function(best, curr){
return best.length > curr.length ? best : curr;
});
console.log(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment