Created
March 17, 2021 23:27
-
-
Save SourceBoy/7ff3ec25162e0901389cad5f0895bb0b to your computer and use it in GitHub Desktop.
Run Length Encoding
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
/** | |
* Run Length Encoding | |
* @author SourceBoy | |
* @license MIT | |
*/ | |
function runlengthEncode(input = '') { | |
const chars = input.split('').concat([' ']); | |
const out = []; | |
let [current] = chars; | |
let i = 0; | |
for (const c of chars) { | |
if (c === current) { | |
i++; | |
} else { | |
out.push(`${i}${current}`); | |
i = 1; | |
} | |
current = c; | |
} | |
return out.join(''); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment