Input:
S = "geeksforgeeks"
Output: g
Explanation: g, e, k and s are the repeating
characters. Out of these, g occurs first.
const repeatedCharacter = (str) => {
for (let i = 0; i < str.length - 1; i++) {
for (let j = i + 1; j < str.length; j++) {
if (str[i] === str[j]) {
return str[i];
}
}
}
return -1
};
console.log(repeatedCharacter("geeksforgeeks"));
/* Time complexity of solution: O(n) */
const getRepeatedChars = (str) => {
const chars = {};
for (const char of str) {
chars[char] = (chars[char] || 0) + 1;
}
return Object.entries(chars).filter(char => char[1] > 1).map(char => char[0]);
}
getRepeatedChars("aabbkdndiccoekdczufnrz"); // ["a", "b", "c", "d", "k", "n", "z"]
function hasRepeats (str) {
return /(.).*1/.test(str);
}