Created
December 6, 2015 06:15
-
-
Save ariutta/41923d8342fe2316cbbd 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
var input = [1, 1, 1, 2, 2, 2, 3, 3]; | |
var outputExpected = [[1, 1, 1], [2, 2, 2], [3, 3]]; | |
var outputActual = input.reduce(function(acc, currentItem) { | |
var accLength = acc.length; | |
var currentGroup = acc[accLength - 1]; | |
var currentGroupLength = currentGroup.length; | |
if (currentGroupLength > 0) { | |
var previousItem = currentGroup[currentGroupLength - 1]; | |
if (previousItem === currentItem) { | |
currentGroup.push(currentItem); | |
} else { | |
acc.push([currentItem]); | |
} | |
} else { | |
currentGroup.push(currentItem); | |
} | |
return acc; | |
}, [[]]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment