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

    Function modeOrDefault

    • Returns the element that appears most frequently in source, or null when the iterable is empty.

      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 | null

      The first most frequent element, or null when source contains no elements.

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

      Unlike mode, this function communicates the absence of elements by returning null. When multiple keys share the maximum frequency, the element encountered first is returned.

      const winner = modeOrDefault<number>([]);
      console.log(winner); // null