count duplicate word in js code example
Example 1: how to check for duplicate syntax in javascript
let strArray = [ "q", "w", "w", "w", "e", "i", "u", "r"];
let findDuplicates = arr => arr.filter((item, index) => arr.indexOf(item) != index)
console.log(findDuplicates(strArray))
console.log([...new Set(findDuplicates(strArray))])
Example 2: 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");