unique
Returns a new array with duplicate elements removed. For arrays of objects, you can specify a property to determine uniqueness.
Import
Section titled “Import”import { unique } from "1o1-utils";import { unique } from "1o1-utils/unique";Signature
Section titled “Signature”function unique<T>({ array, key }: UniqueParams<T>): T[]Parameters
Section titled “Parameters”| Name | Type | Required | Description |
|---|---|---|---|
| array | T[] | Yes | The array to deduplicate |
| key | keyof T | No | Property to determine uniqueness for objects |
Returns
Section titled “Returns”T[] — A new array with duplicates removed.
Examples
Section titled “Examples”unique({ array: [1, 2, 2, 3, 1] });// => [1, 2, 3]
const users = [ { id: 1, name: "Alice" }, { id: 2, name: "Bob" }, { id: 1, name: "Alice Copy" },];
unique({ array: users, key: "id" });// => [{ id: 1, name: "Alice" }, { id: 2, name: "Bob" }]Edge Cases
Section titled “Edge Cases”- Throws if
arrayis not an array. - Without
key, usesSetfor primitive deduplication. - With
key, keeps the first occurrence of each unique value.
Also known as
Section titled “Also known as”deduplicate, distinct, uniq, remove duplicates
Prompt suggestion
Section titled “Prompt suggestion”I'm using 1o1-utils (npm: https://www.npmjs.com/package/1o1-utils, GitHub: https://github.com/pedrotroccoli/1o1-utils, LLM context: https://pedrotroccoli.github.io/1o1-utils/llms.txt). Show me how to use unique to deduplicate an array of objects by ID