Recommended Free Tools
For an array of primitive values, convert it to a Set and back: const unique = [...new Set(values)];. This returns a new array containing the first occurrence of each value, in its original order. For objects, choose the field or rule that defines a duplicate; a Set does not compare object contents.
Remove duplicate primitive values with a Set
For strings, numbers, booleans, and other primitive values, JavaScript’s built-in Set is the simplest default:
As an Amazon Associate I earn from qualifying purchases.
const values = ["a", "b", "a", "c"];
const unique = [...new Set(values)];
console.log(unique); // ["a", "b", "c"]
Array.from(new Set(values)) produces the same result. Both forms create a new array; they do not sort, splice, or otherwise modify the input. A Set iterates in insertion order, so the result keeps the first occurrence of each distinct value.
What counts as a duplicate?
A Set uses SameValueZero equality. For ordinary primitive values, this works like ===, with two notable details: NaN is considered equal to itself, and positive and negative zero are considered equal. Thus, multiple NaN values collapse to one.
#1 Best Overall
Objects and arrays are different: a Set compares them by reference, not by their contents. Two separately created objects such as { id: 1 } are distinct entries, even though their fields match. If records are duplicates because they share an ID or another field, deduplicate using that field.
Deduplicate records by a field
This function keeps the first record encountered for each ID and preserves the order of those retained records:
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
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;
}
Change the field and key type to match your records. The code makes the policy explicit: the first item for an ID wins. If you need the last record instead, use an explicit update strategy, such as a Map keyed by ID, and decide whether the output should follow first-seen key order or the order of the last occurrences.
When another approach makes sense
Filter with indexOf
A familiar primitive-array idiom is values.filter((value, index) => values.indexOf(value) === index). It does not handle NaN the same way as a Set: indexOf(NaN) cannot find a matching value. Prefer Set unless you specifically need different equality behavior.
Use a custom comparer
If duplicates are defined by a custom comparison rule, or you need sorted output, sort and then remove adjacent duplicates with a comparer. TypeScript’s 4.3 release notes show a comparer-based makeUnique example that uses sort and splice; those operations mutate the array. To preserve the original input, sort a copy first, for example with const sorted = [...items].sort(compare), and deduplicate that copy.
TypeScript configuration and runtime support
Deduplication with Set is JavaScript runtime behavior; TypeScript provides the static types. If the compiler reports an unknown Set type or an iterable/spread error, check the project’s TypeScript version and target and lib settings, as well as the JavaScript runtime that executes the code. Do not assume every project configuration supports the same syntax or built-in declarations.
MDN describes Set as widely available across browsers since July 2015. That browser-availability statement does not by itself establish support in every runtime or guarantee that a project’s TypeScript configuration includes the necessary declarations.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePerformance: what can and cannot be concluded
MDN says a Set’s has check is, on average, quicker than checking most previously added values with Array.prototype.includes when both collections have the same size. The JavaScript specification requires average access times to be sublinear, but this is not a benchmark for every deduplication workload and does not establish a universal speedup. Choose the approach for its equality and ordering behavior first, and measure your own workload if performance is the deciding factor.
Quick Recap
Best Value
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.




