In JavaScript:
const uniqueArray = [...new Set(arrayWithDuplicates)];
This one-liner uses the Set
object, which is a built-in JavaScript object that allows you to store unique values of any type, including primitive values and object references. By passing the arrayWithDuplicates
array to the Set
constructor, we create a new Set
object containing only the unique elements of the array. We then use the spread syntax (...
) to convert the Set
object back to an array, which gives us an array with duplicates removed.
The result of this one-liner is assigned to the uniqueArray
variable, which holds the new array with duplicates removed.
This one-liner is a concise and efficient way to remove duplicates from an array in JavaScript, using the built-in Set
object and the spread syntax to convert the Set
object back to an array. It avoids the need for a loop or conditional statements to find and remove duplicates, and can be easily adapted to handle other types of array operations that rely on built-in objects.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.