unique
EditRemoves duplicate values while preserving their first-seen order.
/** Removes duplicate values while preserving their first-seen order. */export const unique = <const T>(array: readonly T[]): T[] => { const seen = new Set<T>(); const result: T[] = []; for (let index = 0; index < array.length; index += 1) { if (!Object.hasOwn(array, index)) continue; const value = array[index] as T; if (seen.has(value)) continue; seen.add(value); result.push(value); } return result;};Download
Section titled “Download”wget -O src/lib/unique.ts https://raw.githubusercontent.com/jrTilak/lazykit/HEAD/registry/functions/unique.tstemp_dir="$(mktemp -d)" && bunx degit jrTilak/lazykit/registry/functions "$temp_dir" && mv "$temp_dir/unique.ts" src/lib/unique.ts && rm -r "$temp_dir"Examples
Section titled “Examples”import { unique } from "./unique";
const arr = [1, 2, 3, 3];const result = unique(arr);console.log(result);// Expected Output: [1, 2, 3]array(readonly T[]) — Values to deduplicate usingSetequality semantics. Empty slots are skipped.
Returns
Section titled “Returns”T[] — A new array containing the first occurrence of each value.