Created
August 14, 2018 00:14
-
-
Save Samuelachema/76474a6b75c68c3f336957ed3cbf5407 to your computer and use it in GitHub Desktop.
Write a function which will take one string argument containing characters between a-z, and should remove all repeated characters (duplicates) in the 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
/* | |
Write a function which will take one string argument containing characters between a-z, and should remove all repeated characters (duplicates) in the string. | |
Python | |
The function should be called remove_duplicates and should return a tuple with two values: | |
A new string with only unique, sorted characters. | |
The total number of duplicates dropped. | |
For example: | |
remove_duplicates('aaabbbac') => ('abc', 5) | |
remove_duplicates('a') => ('a', 0) | |
remove_duplicates('thelexash') => ('aehlstx', 2) | |
JavaScript | |
The function should be called removeDuplicates and eturn an object literal containing a 'uniques' property, which should be the sorted input string but without any duplicates or special characters. | |
The returned object should also have a 'duplicates' propoerty which should represent the total number of duplicate characters dropped | |
For example | |
removeDuplicates('th#elex_ash?') | |
Returns | |
{uniques: 'aehlstx', duplicates: 2} | |
*/ | |
function removeDuplicates(str){ | |
let uniques = str.split('').sort().filter(function(item, pos, self) { | |
return self.indexOf(item) == pos; | |
}).join(''); | |
let duplicates = str.length - uniques.length; | |
uniques = uniques.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, ''); | |
return {'uniques':uniques, 'duplicates': duplicates}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment