Skip to content

Instantly share code, notes, and snippets.

@Daniel-Hug
Created October 13, 2017 01:21
Show Gist options
  • Save Daniel-Hug/e2fd4dc21cd8887bded6a4380821adf8 to your computer and use it in GitHub Desktop.
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)
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