Skip to content

Instantly share code, notes, and snippets.

@SourceBoy
Created March 17, 2021 23:27
Show Gist options
  • Save SourceBoy/7ff3ec25162e0901389cad5f0895bb0b to your computer and use it in GitHub Desktop.
Save SourceBoy/7ff3ec25162e0901389cad5f0895bb0b to your computer and use it in GitHub Desktop.
Run Length Encoding
/**
* 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