Back to all snippets
Library/JavaScript/Get Unique Array Values
javascriptbeginnerarraysutilities

How to implement Get Unique Array Values in Javascript

Remove duplicate values from an array

Quick Answer

Spreading a Set over an array is the simplest way to remove duplicates from a flat array in JavaScript. For arrays of objects, use a Map keyed by a unique property.

Code Snippet

1const unique = (arr) => [...new Set(arr)];
2
3// Or for array of objects
4const uniqueBy = (arr, key) => {
5  return [...new Map(arr.map(item => [item[key], item])).values()];
6};

What is Get Unique Array Values?

Removing duplicates from arrays is a common task in JavaScript. Using the Set data structure provides the most efficient way to get unique values. For arrays of objects, we use Map to deduplicate based on a specific property.

How It Works

  1. 1new Set(arr) stores only unique values — duplicates are automatically discarded.
  2. 2The spread operator converts the Set back into a plain array.
  3. 3For objects, Map stores entries keyed by the chosen property, overwriting earlier duplicates.
  4. 4Spreading Map.values() produces the final deduplicated array.

Common Use Cases

  • Data cleanup — remove duplicate entries from datasets
  • Tag systems — ensure unique tags or categories
  • User lists — deduplicate user IDs or emails
  • Filter results — remove duplicate search results

Key Benefits

  • Fast and efficient with O(n) time complexity
  • Works with primitives and objects
  • Clean one-liner syntax
  • Native JavaScript solution

Common Mistakes to Avoid

  • Using indexOf in a loop — O(n²) compared to Set's O(n).
  • Applying the Set trick to objects — object references are unique even if their contents are identical.
  • Forgetting that Set preserves insertion order, which may differ from the original sort.

Quick Tips

  • Click the "Copy" button above to copy the code to your clipboard
  • This code is production-ready and can be used in your projects immediately
  • Check out related snippets below for more javascript examples

Frequently Asked Questions

Does Set preserve the order of elements?

Yes. Set iterates values in insertion order, so the first occurrence of each value is kept.

How do I deduplicate objects by multiple keys?

Stringify the relevant keys into a composite string and use that as the Map key.

What is the time complexity of the Set approach?

O(n) — each element is visited once when building the Set.

About This Javascript Code Snippet

This free javascript code snippet for get unique array values is production-ready and copy-paste friendly. Whether you are building a web app, API, or frontend interface, this beginner-level example will help you implement get unique array values quickly and correctly.

All snippets in the Snippetly library follow javascript best practices and are tested for real-world use. You can adapt this code to work with React, Vue, Node.js, or any project that uses javascript.

Tags: arrays, utilities  | Language: javascript  | Difficulty: beginner  | Category: JavaScript

Build Your Own Snippet Library

Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.