In JavaScript, removing duplicate elements from an array can be achieved through variety of methods, including the higher order array methods such as reduce(), filter(), or with a straightforward for loop. However, an even more easier approach exists by using the inherent Set object.
A Set cannot contain duplicate values and can be easily initialized from the values of an array. Then, since it is iterable in itself, we can use the spread operator (...) to convert it back to an array of just the unique values easily.
index.js
const uniqueElements = arr => [...new Set(arr)];
uniqueElements([1, 2, 2, 3, 4, 4, 5]); // output: [1, 2, 3, 4, 5]
The Set object lacks of a length property; however, it does have a size property instead. This attribute can be used to determine whether an array contains duplicates.
index.js
const duplicatesExist = arr => arr.length !== new Set(arr).size;
duplicatesExist([1, 2, 2, 3, 4, 4, 5]); // true
duplicatesExist([1, 2, 3, 4, 5]); // false
For the truthy check enjoyers, we can check whether all the values of an array are distinct by inverting the condition.
index.js
const isAllDistinct = arr => arr.length === new Set(arr).size;
isAllDistinct([1, 2, 2, 3, 4, 4, 5]); // false
isAllDistinct([1, 2, 3, 4, 5]); // true
To keep only unique values within an array, we can use the filter() method. Since duplicated elements must exist in multiple indices, we can use indexOf() and lastIndexOf() to validate this. For arrays with numerous duplicates, preemptively creating a Set from the array could enhance performance.
index.js
const filterUnique = arr =>
[...new Set(arr)].filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
filterUnique([1, 2, 2, 3, 4, 4, 5]); // [1, 3, 5]
For the falsy enjoyers, we can also do the opposite, and remove all unique values. In this case the two indices have to be the same.
index.js
const removeUnique = arr =>
[...new Set(arr)].filter(i => arr.indexOf(i) !== arr.lastIndexOf(i));
removeUnique([1, 2, 2, 3, 4, 4, 5]); // [2, 4]
Set is a developer experience booster object when it comes to deal with array with non-unique elements.
Thank you for reading this far. See you in the next chapter :)