Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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