Created
October 13, 2017 01:21
-
-
Save Daniel-Hug/e2fd4dc21cd8887bded6a4380821adf8 to your computer and use it in GitHub Desktop.
JS function: count number of numer of items in each run (strings of identical items)
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
function getRunLengths(array) { | |
var runLengths = []; | |
var curValue; | |
for (var i = 0; i < array.length; i++) { | |
if (array[i] === curValue) { | |
runLengths[runLengths.length - 1]++; | |
} | |
else { | |
runLengths.push(1); | |
curValue = array[i]; | |
} | |
} | |
return runLengths; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment