Alternate solution with Counter from collections

Hi guys,

I was introduced to a module from the Python Standard Library called collections in a leetcode question. It’s Counter() is so powerful it almost feels like cheating. :joy:

Here’s an alternated solution for this code challenge:

# provided input
values = [10, 20, 30, 10, 30, 10] 
# answer to this input: 3, 1

from collections import Counter

def most_least_frequent(values):
    counter = Counter(values)
    most_freq = counter.most_common(1)[0][1]
    least_freq = counter.most_common()[-1][1]
    
    return most_freq, least_freq

most_least_frequent(values)
5 Likes