Skip to content

Instantly share code, notes, and snippets.

@dobakay
Created March 14, 2016 14:12
Show Gist options
  • Save dobakay/44fc630813b0c6ed7db0 to your computer and use it in GitHub Desktop.
Save dobakay/44fc630813b0c6ed7db0 to your computer and use it in GitHub Desktop.
Remove duplicated characters from string
/*
Example input => output
aaaaaaaaaaaaaaaabc => abc
aaaaabbbac => abac
*/
function removeDuplicates(str) {
if(str === null || str === undefined) {
return null
}
if(str.length <= 1) {
return str;
}
var result = [];
result.push(str[0]);
for (var i = 1; i < str.length; i++) {
if(result[result.length-1] !== str[i]) {
result.push(str[i]);
}
};
return result.join('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment