Skip to content

Instantly share code, notes, and snippets.

@kolharsam
Created March 26, 2020 15:52
Show Gist options
  • Save kolharsam/362111c448df439682fb354482b23434 to your computer and use it in GitHub Desktop.
Save kolharsam/362111c448df439682fb354482b23434 to your computer and use it in GitHub Desktop.
Cassidy's Interview Question - 23/03/20
const compress = sortedArr => {
let currentLetterIndex = 0, letterCounter = 0, iter = 0;
// if we one of each letter
let maxIterations = sortedArr.length * 2;
while(iter < maxIterations) {
if (sortedArr[iter] === sortedArr[currentLetterIndex]) {
letterCounter++;
iter++;
} else {
// replace with the count
sortedArr.splice(currentLetterIndex+1, letterCounter-1, `${letterCounter}`);
// reset counters
currentLetterIndex+=2;
letterCounter = 1;
iter = currentLetterIndex+1;
}
}
return sortedArr;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment