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

    Function distinctUntilChanged

    • Removes consecutive duplicate elements by comparing each element with its predecessor.

      Type Parameters

      • TElement

        Type of elements within the source iterable.

      Parameters

      • source: Iterable<TElement>

        The source iterable.

      • Optionalcomparator: EqualityComparator<TElement, TElement>

        Optional equality comparator used to determine whether adjacent elements are equal. Defaults to the library's standard equality comparison.

      Returns IEnumerable<TElement>

      A sequence that yields the first element of each run of equal values.

      Unlike distinct, this only filters adjacent duplicates and preserves earlier occurrences of repeated values.

      const numbers = [1, 1, 2, 2, 2, 1, 3, 3];
      const distinctUntilChangedNumbers = distinctUntilChanged(numbers).toArray();
      console.log(distinctUntilChangedNumbers); // [1, 2, 1, 3]