Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
"The Most Frequent": two solutions - max with key || Counter.most_common solution in Clear category for The Most Frequent by dan_s
from collections import Counter
# Solution #1
def most_frequent(data: list) -> str:
return max(el:=Counter(data), key=el.get)
"""
Solution #2
def most_frequent(data: list) -> str:
return Counter(data).most_common(1)[0][0]
"""
if __name__ == "__main__":
# These "asserts" using only for self-checking and not necessary for auto-testing
print("Example:")
print(most_frequent(["a", "b", "c", "a", "b", "a"]))
assert most_frequent(["a", "b", "c", "a", "b", "a"]) == "a"
assert most_frequent(["a", "a", "bi", "bi", "bi"]) == "bi"
print("Done")
April 15, 2022