find duplicate letters in string javascript code example
Example 1: how to find repeated characters in a string in javascript
const getRepeatedChars = (str) => {
const strArr = [...str].sort();
const repeatedChars = [];
for (let i = 0; i < strArr.length - 1; i++) {
if (strArr[i] === strArr[i + 1]) repeatedChars.push(strArr[i]);
}
return [...new Set(repeatedChars)];
};
getRepeatedChars("aabbkdndiccoekdczufnrz");
Example 2: duplicate characters in a string javascript
const text = 'abcda'.split('')
text.some( (v, i, a) => {
return a.lastIndexOf(v) !== i
})