HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Get all unique values in a JavaScript array & remove duplicates

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptremoveallduplicatesgetuniquearrayvalues

Problem

Removing duplicates from an array in JavaScript can be done in a variety of ways, such as using Array.prototype.reduce(), Array.prototype.filter() or even a simple for loop. But there's a much simpler way to do it, using the built-in Set object.
A Set cannot contain duplicate values and can be easily initialized from the values of an array. Then, as it is iterable in itself, we can use the spread operator (...) to convert it back to an array of just the unique values.
Set doesn't have a length property, but it does have a size property, instead. We can use this to check if an array contains duplicates.
Inverting the condition, we can check if all the values of an array are distinct.
If we want to keep only values that are not duplicated, we can use the Array.prototype.filter() method. Elements that appear more than once have to appear in at least two different indexes, so we can use Array.prototype.indexOf() and Array.prototype.lastIndexOf() to check for this. If we expect the array to have many duplicate values, creating a Set from it first may improve performance.

Solution

const uniqueElements = arr => [...new Set(arr)];

uniqueElements([1, 2, 2, 3, 4, 4, 5]); // [1, 2, 3, 4, 5]


Set doesn't have a length property, but it does have a size property, instead. We can use this to check if an array contains duplicates.
Inverting the condition, we can check if all the values of an array are distinct.
If we want to keep only values that are not duplicated, we can use the Array.prototype.filter() method. Elements that appear more than once have to appear in at least two different indexes, so we can use Array.prototype.indexOf() and Array.prototype.lastIndexOf() to check for this. If we expect the array to have many duplicate values, creating a Set from it first may improve performance.
We can also do the opposite, and remove all values that appear only once. In this case the two indices have to be the same. Notice that using a Set for this operation will drop duplicates from the result.
More complex data, such as objects, can't be compared using equality comparison, so we need to use a function to check for duplicates. Set objects are not much use here, so we can use Array.prototype.reduce() and Array.prototype.some() to manually populate a new array with only the unique values. Using array methods, we can also check if an array contains duplicates, or remove all values that appear more than once.

Code Snippets

const uniqueElements = arr => [...new Set(arr)];

uniqueElements([1, 2, 2, 3, 4, 4, 5]); // [1, 2, 3, 4, 5]
const hasDuplicates = arr => arr.length !== new Set(arr).size;

hasDuplicates([1, 2, 2, 3, 4, 4, 5]); // true
hasDuplicates([1, 2, 3, 4, 5]); // false
const allDistinct = arr => arr.length === new Set(arr).size;

allDistinct([1, 2, 2, 3, 4, 4, 5]); // false
allDistinct([1, 2, 3, 4, 5]); // true

Context

From 30-seconds-of-code: unique-values-in-array-remove-duplicates

Revisions (0)

No revisions yet.