Created
July 21, 2015 17:27
-
-
Save DavidBruant/77ad4c4418ac8619c5d9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"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