Without any imports, you can use a regular dictionary.
def checkio(text: str) -> str:
lowercase_input = text.lower()
x = {}
for i in lowercase_input:
x[i] = x.get(i, 0) + 1
return max(x.items(), key=lambda x: x[1])
key, value = checkio('asdafsasaaaa') # ('a', 7)
Explanation
dict.get
has an optional second parameter, which we set to 0
. This means if the key does not exist in the dictionary x.get(i, 0)
will return 0
. max
has an optional key
parameter which accepts an anonymous (lambda
) function. Since dict.items()
returns an iterable of (key, value)
pairs, we can calculate the maximum by looking at the value
component (1st index).
Performance
This is inefficient versus Counter
+ most_common
as shown by @abc. Specifically, most_common
uses heapq
to reduce time complexity. See also Python collections.Counter: most_common complexity.