Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

how to find repeated characters in a string in javascript

/* 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"]
Source by practice.geeksforgeeks.org #
 
PREVIOUS NEXT
Tagged: #find #repeated #characters #string #javascript
ADD COMMENT
Topic
Name
6+1 =