how to check if a character is repeated in a string javascript code example
Example: 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"); // ["a", "b", "c", "d", "k", "n", "z"]