David Tran

Checking If a Primitive Array is Unique in JavaScript

Looking for a way to verify that your primitive array does not contain duplicates without reaching for a third-party package?

No problem, ES6 provides a simple solution.

Note: This solution only works for primitive arrays. For arrays containing nested objects, you may want to do an additional "deep" check using recursion.


const arr = [ /* ... */ ];
const isUnique = (new Set(arr)).size === arr.length;

Yup, that is it.


Here, we are passing in our array to the Set constructor. The constructor converts the Array object into a Set object.

The Set object differs from an array because it only allows the storage of unique values. As a result, when we construct a Set from our array, all duplicate values are removed.

const arr = [1, 2, 2, 3, 3];
new Set(arr); // Set: [1, 2, 3]

Finally, we check if the new Set's size is equal to the size of the original array. If the sizes are the same, this indicates that the original array is unique as no duplicates were removed.

const arr = [1, 2, 3]; // length: 3
const set = new Set(arr); // Set: [1,2,3] size: 3
isUnique = set.size === arr.length; // true

Note that we use the size property to check for the Set's size while arrays use length.

Final Thoughts

The time complexity of this solution is probably similar to creating a dictionary, iterating through each element, and checking if that element has been seen before. However, this approach provides an abstraction that, in my opinion, is easier to read.

Noticed a mistake in this post? Feel free to submit a pull request!