Array to Set in JavaScript

What is an Array in JavaScript?

In JavaScript, an array is an object that contains a collection of data types that is similar to each other. In other words, an array is a collection of similar data types that is used to store the values or elements.
With the help of an array in JavaScript, we can organize similar types of data and store it. In JavaScript, the first detail of the array is at index zero, and the second element is at index 1 and it goes so on.

Syntax


var array = [1,2,3,4,5];  

What is a Set in JavaScript?

In JavaScript, a set is a data object that holds a collection of unique values. In a set, we cannot contain the duplicate values or elements but we can store any type of data.
In JavaScript, if we have a duplicate or equal value in a set then only the first instance will be saved in the set. In other words, a set is an object that allows us to store the unique values and no element can be repeated.

Syntax


new Set ([it]);   

How to Convert Array to Set in JavaScript?

In JavaScript, if we want to convert array to set then we need to use some methods. Some methods are as follows:

Using map () and add () methods

In JavaScript, with the use of the map () method, we can call a function once for each element without changing the array that is originally present, and with the use of the add () method we can append an element with the value that is specified.

Syntax


array.map (function (current value, index, array), value)  

Let's take an example to understand how the map () method and add () methods work in JavaScript:

Example


let numbers = [1, 2, 3, 4, 4, 5, 6, 6];
let uniqueNumbers = new Set(); // Create an empty Set

numbers.map((value) => uniqueNumbers.add(value)); // Add each element to the Set

console.log(uniqueNumbers); // Output the Set
 

Output

Set(6) { 1, 2, 3, 4, 5, 6 }

Reduce () method

The reduce() method in JavaScript is used to apply a function on each element of the array and accumulate the results. By combining it with the add() method of a Set, we can create a set from an array.

Syntax


array.reduce((acc, currentValue) => acc.add(currentValue), new Set());
        

Let's take an example to understand how the reduce () method works in JavaScript:

Example


let numbers = [1, 2, 3, 4, 4, 5, 6, 6];
let uniqueNumbers = numbers.reduce((acc, num) => acc.add(num), new Set());
console.log(uniqueNumbers);
 

Output

Set(6) { 1, 2, 3, 4, 5, 6 }

Spread () operator

Syntax


let mySet = new Set([...array]);
        

Let's take an example to understand the spread operator in detail with the help of this program.

Example


let numbers = [1, 2, 3, 4, 4, 5, 6, 6];
let uniqueNumbers = new Set([...numbers]);
console.log(uniqueNumbers);
 

Output

Set(6) { 1, 2, 3, 4, 5, 6 }

Key Points on Array to Set Conversion:

Syntax for Converting Array to Set:

Syntax


let myArray = [1, 2, 2, 3, 4, 4];
let mySet = new Set(myArray);

Example of Array to Set Conversion:

This example demonstrates how to convert an array with duplicate values to a set, thereby removing the duplicates.

Example


let numbers = [1, 2, 3, 4, 4, 5, 6, 6];
let uniqueNumbers = new Set(numbers);
console.log(uniqueNumbers);

Output

Set(6) { 1, 2, 3, 4, 5, 6 }

Benefits of Converting Array to Sets

Converting array to sets offers several advantages in JavaScript Development.

Set Operations:

Why Use Sets?

Sets are often more efficient than arrays when you need to store unique values, perform fast lookups, or eliminate duplicates. They are an excellent choice when dealing with large datasets or when uniqueness is a priority.