Created
March 14, 2016 14:12
-
-
Save dobakay/44fc630813b0c6ed7db0 to your computer and use it in GitHub Desktop.
Remove duplicated characters from string
This file contains 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
/* | |
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