maxChar

Easy · String Manipulations · 2 test cases

Given a string, return the character that is most commonly used in the string.

Examples

maxChar('abcccccccd') → 'c'
maxChar('apple 1231111') → '1'

Starter code

function maxChar(str) {

}

Complexity of the optimal solution

Time O(n), space O(k). The function iterates through the string of length n once to build a character map, and then iterates through the map's keys (k). Since k <= n, the overall time complexity is O(n). The space for the map depends on the number of unique characters (k).

Solve maxChar in the browser