• 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

    • Optional filterPredicate: ((element) => 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.

    Example

    count([1, 2, 3]); // 3
    

    Example

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

    See

    frequencyTable to count the occurrences of all elements in an iterable

    Iterable