Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsiTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
For an array of primitive values, use a Set and convert it back to an array: const unique = [...new Set(values)];. This keeps the first occurrence of each value, in order. For objects, decide whether duplicates mean the same object reference or matching values in a chosen field; a plain Set does not compare object contents.
Remove duplicate primitive values with Set
Use the built-in JavaScript Set when its equality rules match your data. Set stores unique values, and iterates them in insertion order, so the resulting array preserves the order of first occurrences.
const values = ["a", "b", "a", "c"];
const unique = [...new Set(values)];
console.log(unique); // ["a", "b", "c"]
You can write the conversion with Array.from instead:
Recommended Free Tools
const unique = Array.from(new Set(values));
Both forms create a new array; neither sorts or splices the input. The MDN Set reference describes Set as storing unique values and notes its broad browser availability since July 2015. Whether the syntax and types work in a particular project still depends on its TypeScript configuration and runtime.
#1 Best Overall
What counts as a duplicate?
Set uses SameValueZero equality. For most primitive values, this behaves like ===; the notable differences are that NaN equals itself and positive and negative zero are considered equal.
const values = [NaN, NaN, 0, -0];
const unique = [...new Set(values)];
console.log(unique); // [NaN, 0]
Objects and arrays are compared by reference, not by their fields. Two separately created objects with the same contents remain distinct:
Rank #2
- TypeScript implements a superset of syntax for strictly typed development, facilitating deep static analysis and enhanced development environment integration. The compiler translates source into standard script formats, ensuring parity across any runtime.
- TypeScript is ideal for front-end developers, full-stack engineers, and software architects who build large-scale web applications. It serves those looking to improve code excellence, reduce bugs through static checking, and maintain complex projects more.
- Lightweight, Classic fit, Double-needle sleeve and bottom hem
const values = [{ id: 1 }, { id: 1 }];
const unique = [...new Set(values)];
console.log(unique.length); // 2
That behavior is useful when duplicate means “the exact same object instance.” When records with the same key should count as duplicates, deduplicate by that key instead.
Deduplicate records by a field
This implementation keeps the first record for each id, while retaining the order in which those records first appeared:
function uniqueById<T extends { id: string | number }>(items: T[]): T[] {
const seen = new Set<string | number>();
const result: T[] = [];
for (const item of items) {
if (seen.has(item.id)) continue;
seen.add(item.id);
result.push(item);
}
return result;
}
Use the appropriate field and key type for your data. For example, if records are duplicates when their email addresses match, use email as the Set key. This version returns the original record objects; it does not clone or modify them.
If the last record for each key must win, make that policy explicit with a Map. Updating a key replaces its value, while the key retains its original insertion position:
function lastById<T extends { id: string | number }>(items: T[]): T[] {
const byId = new Map<string | number, T>();
for (const item of items) byId.set(item.id, item);
return [...byId.values()];
}
When to use another approach
Filter and indexOf
A filter and indexOf pattern can work for ordinary primitive arrays, but it handles NaN differently: indexOf(NaN) does not find a NaN value. Set treats NaN values as equal, so it is generally the clearer built-in choice unless you need different comparison behavior.
Free tools Windows power users keep installed
One-click scans. No signup required.
Custom comparison or sorted output
Use a comparer when your definition of equality cannot be expressed as a key. TypeScript’s 4.3 release notes show a comparer-based makeUnique example that sorts values and removes adjacent duplicates. Sorting and splicing mutate the array, so copy it first if the caller’s input must remain unchanged:
Best Value
const sorted = [...items].sort(comparer);
// Remove adjacent duplicates from sorted, or use a comparer-based scan.
That approach produces comparer-sorted output rather than preserving the original order of first occurrences.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.TypeScript configuration and performance
Deduplication with Set is JavaScript runtime behavior; TypeScript provides types for arrays, sets, and iterables. If the compiler flags Set or spread conversion, check the project’s target, lib, TypeScript version, and runtime support. The TypeScript documentation covers generic types such as arrays and iterables and spread conversion.
Set is not guaranteed to be faster for every workload. MDN notes that Set’s average access time must be sublinear in collection size and says its has check is, on average, quicker than checking most previously added values with Array.prototype.includes when the array and set are the same size. This is not a workload-specific benchmark or a promise of a particular speedup.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

