Skip to content

Instantly share code, notes, and snippets.

@nikgoy
Created June 4, 2020 08:34
Show Gist options
  • Save nikgoy/b6ff3b66283f3657de62da687609e7ce to your computer and use it in GitHub Desktop.
Save nikgoy/b6ff3b66283f3657de62da687609e7ce to your computer and use it in GitHub Desktop.
// remove duplicates
crunch("ddaaiillyy ddoouubbllee"); // "daily double"
crunch("4444abcabccba"); // "4abcabcba"
crunch("ggggggggggggggg"); // "g"
crunch("a"); // "a"
crunch(""); // ""
function crunch(string) {
let letters = string.split("");
for (let i = 0; i < letters.length; i++) {
if (letters[i] === letters[i + 1]) {
letters.splice(i, 1);
}
}
console.log(letters.join(''));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment