uniq()
uniq<
T>(array):T[]
Creates a duplicate-free version of an array.
Uses strict equality (===). First occurrence wins.
Type Parametersβ
T: Tβ
The type of elements in the array.
Parametersβ
array: readonly T[]β
The array to inspect.
Returns: T[]β
A new array with unique values, preserving order of first occurrence.
Sinceβ
2.0.0
Performanceβ
O(nΒ·k) linear scan for small arrays (β€200), O(n) with Set for larger ones.
Also known asβ
dedupe (Effect) Β· uniq (Lodash, es-toolkit, Remeda, Ramda, Antfu) Β· unique (Radashi, Modern Dash)
Exampleβ
uniq([1, 2, 2, 3, 4, 4, 5]);
// => [1, 2, 3, 4, 5]
uniq([11, 2, 3, 44, 11, 2, 3]);
// => [11, 2, 3, 44]
How it works?β
Removes duplicates using strict equality (===). First occurrence wins.
Use Casesβ
Remove duplicate values from a collection πβ
Remove repeated values from an array, keeping only the first occurrence. Essential for cleaning up lists of IDs, tags, or any primitive values.
const selectedIds = [3, 7, 3, 12, 7, 5, 12];
const uniqueIds = uniq(selectedIds);
// => [3, 7, 12, 5]
Clean up user tags πβ
Ensure a tag list contains no repeated entries after user input. Perfect for form handling, content tagging, or search filters.
const userTags = ["react", "typescript", "react", "nodejs", "typescript"];
const cleanTags = uniq(userTags);
// => ["react", "typescript", "nodejs"]
Collect unique categories from a datasetβ
Extract all distinct values after mapping a property from objects. Useful for building filter menus or faceted navigation.
const products = [
{ name: "Laptop", category: "electronics" },
{ name: "Shirt", category: "clothing" },
{ name: "Phone", category: "electronics" },
{ name: "Pants", category: "clothing" },
{ name: "Tablet", category: "electronics" },
];
const categories = uniq(products.map((p) => p.category));
// => ["electronics", "clothing"]
Deduplicate merged search results from multiple sourcesβ
Remove duplicates when combining results from multiple search providers. Common when aggregating results from Algolia, Elasticsearch, and local DB.
const algoliaResults = ["doc-1", "doc-3", "doc-5", "doc-7"];
const dbResults = ["doc-2", "doc-3", "doc-4", "doc-5"];
const allResults = uniq([...algoliaResults, ...dbResults]);
// => ["doc-1", "doc-3", "doc-5", "doc-7", "doc-2", "doc-4"]
Ensure idempotent event processingβ
Collect unique event IDs to avoid processing the same event twice. Critical for webhook handlers, message queues, and event-driven architectures.
const processedEventIds: string[] = [];
function handleEvent(eventId: string, payload: unknown) {
const seen = uniq([...processedEventIds, eventId]);
if (seen.length === processedEventIds.length) {
return; // Already processed, skip
}
processedEventIds.push(eventId);
processPayload(payload);
}