1/* Time complexity of solution: O(n) */
2const getRepeatedChars = (str) => {
3 const chars = {};
4 for (const char of str) {
5 chars[char] = (chars[char] || 0) + 1;
6 }
7 return Object.entries(chars).filter(char => char[1] > 1).map(char => char[0]);
8}
9
10getRepeatedChars("aabbkdndiccoekdczufnrz"); // ["a", "b", "c", "d", "k", "n", "z"]
1const str = "big black bug bit a big black dog on his big black nose";
2const findDuplicateWords = str => {
3 const strArr = str.split(" ");
4 const res = [];
5 for(let i = 0; i < strArr.length; i++){
6 if(strArr.indexOf(strArr[i]) !== strArr.lastIndexOf(strArr[i])){
7 if(!res.includes(strArr[i])){
8 res.push(strArr[i]);
9 };
10 };
11 };
12 return res.join(" ");
13};
14console.log(findDuplicateWords(str));