Function to count duplicate characters in a string

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Function to count duplicate characters in a string



I am trying to write a function which finds all unique characters in a provided string.



I'd like the function to return the results in the following format:


removeDuplicates('th#elex_ash?') -> unique: 'aehlstx', duplicates: 2



So far I have come up with the following attempted solution:


function removeDuplicates(str)
var unique ="";
for (var i=0; i<str.length; i++)
if(unique.indexOf(str[i]) == -1)
unique += str[i];


unique = unique.replace(/[&/\#,_+()$~%.'":*?<>]/g, '');
return unique.split('').sort().join('');






console.log(removeDuplicates('aaabbbac'));
console.log(removeDuplicates('a'));
console.log(removeDuplicates('th#elex_ash?'));




3 Answers
3



If you are writing for an environment where you can use newer javascript features. Set makes this a little easier since it will enforce uniqueness among the contents. You can also compare the lengths of the cleaned string with the uniques to find the duplicate count


Set



For example:




function removeDuplicates(str)
str = str.replace(/[&/\#,_+()$~%.'":*?<>]/g, '')
let unique = Array.from(new Set(str))
.sort((a, b) => a.localeCompare(b))
.join('')

let duplicates = str.length - unique.length
return unique, duplicates


console.log(removeDuplicates("th#elex_ash?"))





Thanks @PatrickRoberts — I knew that and just wasn't thinking. I'll edit…
– Mark Meyer
Aug 12 at 3:50


function removeDuplicates(str)
var returnObject =
unique : "",
duplicates : 0
;

for (var i = 0; i < str.length; i++)
if (returnObject.unique.indexOf(str[i]) < 0)
returnObject.unique += str[i];
else
returnObject.duplicates++;



returnObject.unique = returnObject.unique.replace(/[&/\#,_+()$~%.'":*?<>]/g, '');
returnObject.unique = returnObject.unique.split('').sort().join('');

return returnObject;


console.log(removeDuplicates('aaabbbac'));
console.log(removeDuplicates('a'));
console.log(removeDuplicates('th#elex_ash?'));





Thanks @pseudobbs, exactly what i was looking to do
– Assurance Femi
Aug 12 at 3:19





No problem! Can you mark as correct or upvote? I'm new on SO and trying to build reputation
– pseudobbs
Aug 12 at 3:24





Still having issues
– Assurance Femi
Aug 12 at 4:38





Return object should have a 'uniques' string property. Return object should have a 'duplicates' number property. 'uniques' should be 'aehlstx'
– Assurance Femi
Aug 12 at 4:39






so change the name of the string property from unique to uniques
– pseudobbs
Aug 12 at 4:49



unique


uniques



You are very close!



An object can be returned in Javascript just like anything else; for instance, to return the format you were asking for, simply do the following:


return
unique: unique,
duplicates: duplicates



Notice how your variable names are the same as your property keys? You can use a shorthand notation by simply writing unique and duplicates, rather than unique: unique and duplicates: duplicates.


unique


duplicates


unique: unique


duplicates: duplicates



You're not keeping a count of duplicates. To do so, simply add an else block to your if(unique.indexOf(str[i]) == -1) conditional, which should only be reached when a character is indeed a duplicate, and increment a counter - lets name it duplicates.


else


if(unique.indexOf(str[i]) == -1)


duplicates



Complete Solution




function removeDuplicates(str)
var unique = "";
var duplicates = 0;

for (var i = 0; i < str.length; i++)
if (unique.indexOf(str[i]) == -1)
unique += str[i];
else
duplicates++;


unique = unique.replace(/[&/\#,_+()$~%.'":*?<>]/g, '').split('').sort().join('')

return
unique,
duplicates




const testString = 'th#elex_ash?';
const expectedOutput =
unique: 'aehlstx',
duplicates: 2
;

console.log(removeDuplicates(testString));






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered