I'm trying to check what letters repeat in a string by creating a new array of only the repeated letters using the .filter()
method but I only want the letter to appear one time, no matter how many times it repeats.
This is what I tried:
const fullName = "Muhammad Ali";
const fullNameLowercase = fullName.toLowerCase();
const splitName = fullNameLowercase.split("");
let repeats = splitName.filter((letter, index) => {return splitName.indexOf(letter) !== index});
console.log(repeats); // prints [ 'm', 'm', 'a', 'a' ]
I want it to only add the letter once to the array repeats
, how do I do it? Is there any other more efficient way to do what I want that doesn't use .filter()
?
Comments
Post a Comment