how to find duplicate words in a 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"); // ["a", "b", "c", "d", "k", "n", "z"]

Example 2: find repeated words in paragrah a string javascript

const str = "big black bug bit a big black dog on his big black nose";
const findDuplicateWords = str => {
   const strArr = str.split(" ");
   const res = [];
   for(let i = 0; i < strArr.length; i++){
      if(strArr.indexOf(strArr[i]) !== strArr.lastIndexOf(strArr[i])){
         if(!res.includes(strArr[i])){
            res.push(strArr[i]);
         };
      };
   };
   return res.join(" ");
};
console.log(findDuplicateWords(str));