• Category

    Count the number of elements in an iterable You may optionally provide a predicate function to filter which elements are counted.

    Time complexity: O(n)

    Space complexity: O(1)

    Type Parameters

    • T

    Parameters

    • iterable: Iterable<T>

      The iterable to count the elements of

    • OptionalfilterPredicate: ((element: T) => boolean)
        • (element): boolean
        • Parameters

          • element: T

          Returns boolean

    Returns number

    The number of elements in iterable. If predicate was provided, the number of elements in iterable that satisfy predicate.

    count([1, 2, 3]); // 3
    
    count([1, 2, 3], number => number % 2 === 1); // 2
    

    frequencyTable to count the occurrences of all elements in an iterable

    Iterable