Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How can I remove duplicates from an array while keeping its order?

Quick answer

In JavaScript, convert the array to a Set and spread it back into an array: const unique = [...new Set(values)];. A Set keeps values in insertion order, so the first occurrence stays in its original position. For objects, it removes a repeated reference to the same object, but not separate objects that merely have matching properties. [S1]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

What is the shortest way to remove duplicates from a JavaScript array without changing the order?

Geez

Use a Set, then spread it into a new array:

const values = ["pear", "plum", "pear", "kiwi", "plum"];
const unique = [...new Set(values)];

console.log(unique); // ["pear", "plum", "kiwi"]

A Set stores unique values and iterates them in insertion order, so each value remains at its first position in the input. [S1] This creates a separate result array; it does not remove entries from values.

The shortcut works when your definition of “duplicate” matches Set's value-equality rules. Objects need a little more care.

Follow-up question

What happens if my array contains objects with matching properties?

Geez

Set compares objects by identity, not by comparing their properties. Two separately created objects remain distinct even if their contents match. If the same object reference appears twice, Set recognizes the repeat. [S1]

const record = { id: 7, name: "Mina" };
const records = [record, { id: 7, name: "Mina" }, record];
const uniqueRecords = [...new Set(records)];

console.log(uniqueRecords.length); // 2

The first and third entries are references to record, so one is removed. The middle entry is a different object, so it remains despite having matching properties. [S1]

If you want one record per id, decide that this is your duplicate rule and use an approach that compares that field. The Set example does not deduplicate records by their contents.

Follow-up question

How can I check that the output keeps the order I expect?

Geez

Try an input with repeats in different positions and compare the full result with the expected array:

const values = ["b", "a", "b", "c", "a"];
const unique = [...new Set(values)];

console.log(unique); // ["b", "a", "c"]

The result keeps each value's first position and omits later occurrences, matching Set's insertion-order behavior. [S1]

For objects, check the two cases separately: repeat the same reference, then include two separately created objects with matching properties. Set removes the repeated reference but retains the separate objects. [S1] These examples help verify your chosen rule; they do not decide whether reference identity or a particular field is right for your application.

Follow-up question

Can you give me a reusable version and a quick way to choose whether it fits?

Geez

You can wrap the Set conversion in a helper:

function uniqueInOrder(values) {
  return [...new Set(values)];
}

const tags = ["news", "how-to", "news"];
const uniqueTags = uniqueInOrder(tags);
// ["news", "how-to"]

The helper returns a new array with the first occurrence of each Set value in iteration order. [S1] For objects, it removes repeated references only; separately created objects with matching properties remain distinct. [S1]

Before using it, state your rule plainly: same value, same object reference, or same chosen field. Use this helper when the first two Set rules match what you mean. If you mean a field such as id, choose an approach that applies that rule and check the resulting order against an example.

A practical checklist

  • Decide whether duplicates mean equal values, the same object reference, or a matching object field.

  • Use [...new Set(values)] when Set's value-equality rules match your requirement.

  • Test an input with repeats in different positions and verify first-occurrence order.

  • For objects, test a repeated reference and separate objects with matching properties.

A few more useful answers

Does this change the original array?

No. [...new Set(values)] creates a new array; it does not remove entries from values.

Will two objects with identical properties be deduplicated?

Not if they are separate object instances. Set recognizes the same object reference, not matching object contents. [S1]

Does the result keep the first or last occurrence?

It keeps each value at its first position in the input's iteration order. [S1]

Sources

References used for this guide.

  1. [S1] Set — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work