Skip to main content

Function: unique()

unique<ElementType>(iterable: IterableResolvable<ElementType>): IterableIterator<ElementType>

Creates an iterable with the unique elements of the input iterable. Under the hood, it calls union with the iterable itself.

Type parameters

Type parameter
ElementType

Parameters

ParameterTypeDescription
iterableIterableResolvable<ElementType>An iterator to remove duplicates from.

Returns

IterableIterator<ElementType>

An iterator that yields the values of the provided iterator with duplicates removed.

Example

import { unique } from '@sapphire/iterator-utilities';

const iterable = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5];
console.log([...unique(iterable)]);
// Output: [1, 2, 3, 4, 5]

Source

projects/utilities/packages/iterator-utilities/src/lib/unique.ts:19