xxxxxxxxxx
const getRepeatedChars = (str) => {
let result = [];
str.map(each => {
let repeatedChars = 0;
for (let i = 0; i < each.length - 1; i++) {
if (each[i] === each[i + 1] && each[i] !== each[i - 1]) {
repeatedChars += 1;
}
}
result.push(repeatedChars);
});
return result;
};
getRepeatedChars(["aaabbbkdnndicccoekdczufnrz", "awsfgds"]); // [4, 0]
xxxxxxxxxx
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"));
xxxxxxxxxx
/* 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"]
xxxxxxxxxx
const text = 'abcda'.split('')
text.some( (v, i, a) => {
return a.lastIndexOf(v) !== i
}) // true