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:
- Map () and add () methods
- Reduce () method
- Spread () operator
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
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
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
Key Points on Array to Set Conversion:
- Set: A set is a collection of values where each value must be unique. Sets are useful when you need to store data without duplicates.
- Array to Set: By converting an array to a set, you automatically remove any duplicate values in the array.
- Constructor: The
Set()
constructor can take an array as an argument to create a new set from its values. - Efficiency: Using sets is more efficient when you need to perform operations like checking for the existence of a value, due to their underlying data structure.
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
Benefits of Converting Array to Sets
Converting array to sets offers several advantages in JavaScript Development.
- Elimination of duplicate elementsIn JavaScript, sets automatically removes the duplicate elements and ensure the data integrity.
- Improved performance In JavaScript, sets provide faster access and lookup times compared to arrays, especially for larger collections.
- Simplified data manipulationIn JavaScript, Sets offer us various built-in method for set operations such as union, intersection and difference.
Set Operations:
- Adding Elements: You can add elements to a set using the
add()
method. - Checking Existence: The
has()
method allows you to check if a particular value exists in the set. - Deleting Elements: You can remove elements from a set using the
delete()
method. - Iterating through Set: You can iterate through the set using
forEach()
orfor...of
loops. - Size: The size of a set is accessible through the
size
property, which returns the number of unique elements in the set.
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.