Files
30-seconds-of-code/snippets/countOccurences.md
2018-01-09 12:47:55 +05:30

426 B

countOccurences

Counts the occurrences of a value in an list.

Uses the reduce functin from built-in module functools to increment a counter each time you encounter the specific value inside the list.

def countOccurences(arr, val):
    return reduce(
        (lambda x, y: x + 1 if y == val and type(y) == type(val) else x + 0),
        arr)

countOccurrences([1, 1, 2, 1, 2, 3], 1) # 3