Created
March 26, 2020 15:52
-
-
Save kolharsam/362111c448df439682fb354482b23434 to your computer and use it in GitHub Desktop.
Cassidy's Interview Question - 23/03/20
This file contains hidden or 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
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