@mirei/ts-collections
    Preparing search index...

    Function mode

    • Returns the element that appears most frequently in source.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      • TKey

        Type of key produced by keySelector.

      Parameters

      • source: Iterable<TElement>

        The source iterable to inspect.

      • OptionalkeySelector: Selector<TElement, TKey>

        Optional selector that projects each element to the key used for frequency counting. Defaults to the element itself.

      Returns TElement

      The first element whose occurrence count matches the maximum frequency.

      Thrown when source is empty.

      Re-throws any error thrown while iterating source or executing keySelector.

      The source iterable is fully enumerated to build frequency counts before the result is determined. When multiple keys share the same frequency, the earliest corresponding element is returned.

      const winner = mode([1, 2, 2, 3]);
      console.log(winner); // 2