Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

Remove Duplicates from JavaScript Arrays

1 min read .
Remove Duplicates from JavaScript Arrays

Removing duplicates depends on what “duplicate” means for your data. Primitive values, objects with unique IDs, and records compared by several properties need different strategies.

Primitive Values with Set

const values = [1, 2, 3, 2, 4, 3];
const unique = [...new Set(values)];

console.log(unique); // [1, 2, 3, 4]

Keep the First Object for Each ID

const items = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 1, name: 'Alice again' },
];

const seen = new Set();
const unique = items.filter((item) => {
  if (seen.has(item.id)) return false;
  seen.add(item.id);
  return true;
});

Keep the Last Object for Each ID

A Map naturally overwrites earlier values for the same key:

const byId = new Map();

for (const item of items) {
  byId.set(item.id, item);
}

const unique = [...byId.values()];

Use a Composite Key

const seen = new Set();
const unique = items.filter((item) => {
  const key = `${item.place}\u0000${item.name}`;
  if (seen.has(key)) return false;
  seen.add(key);
  return true;
});

For complex data, define equality explicitly instead of relying blindly on JSON.stringify(), because property order and non-JSON values can make serialized comparisons misleading.

Conclusion

Use Set for primitive values, and use a Set or Map keyed by the properties that define identity for objects. Make the duplicate rule explicit so the code matches the data model rather than merely comparing whole objects by reference.

Related Posts

chevron-up