How to Convert Array to Set in JavaScript? Last Updated : 25 Oct, 2024 Comments Improve Suggest changes Like Article Like Report The Set constructor is used to convert the array into set in JavaScript. It is useful to clean the array and remove duplicate elements. Converting Array to Set means transforming the array elements into a Set object.Using Set ConstructorThe Set constructor in JavaScript directly converts an array into a Set. This process automatically filters out duplicate elements, creating a collection of unique values. It's a simple and efficient way to manage distinct data in an array.Syntaxlet newSet = new Set( a ) JavaScript // Create array using array literal let a = [1, 1, 2, 2, 2, 2, 5, 5]; // Convert to set using the set constructor let s = new Set(a); // Display the output console.log(s); OutputSet(3) { 1, 2, 5 } This is the simple and fastest method to Convert Array to Set in JavaScript, because the constructor handles the entire array at once. This method is suitable when we have large array size.If you want to handle some additional checks/conditions while converting Array to set, above method may not be best choice. For such cases, you can choose below given method. Using set.add() MethodWe can also convert the array into set by adding the elements one by one. To do this we will iterate the array using forEach loop and push the elements into Set, using set.add() method.Syntaxset.add( value );This add function checks if the value already exist in the set or not. If the value is not found, it pushes the value to set otherwise skips adding the element. JavaScript // Create array using array literal let a = [1, 1, 2, 2, 2, 2, 5, 5]; // Initialize enpty set using the set constructor let s = new Set(); // Iterate over the array and add the elements to set a.forEach( e => s.add(e) ); // Display the output console.log(s); OutputSet(3) { 1, 2, 5 } When you want to manipulate elements dynamically (any check or modification) then this method can be used, else using constructor is recommended. Comment More infoAdvertise with us Next Article How to map, reduce and filter a Set element using JavaScript ? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads Iterate over Set in JavaScript or JS We will iterate over set elements in JavaScript. A set is a collection of unique elements i.e. no element can appear more than once in a set.Below are the approaches to iterate over set elements in JavaScript:Approach 1: Using for...of loopThe forâ¦of loop iterates over the iterable objects (like Arr 3 min read How to get the union of two sets in JavaScript ? A union set is the combination of two elements. In mathematical terms, the union of two sets is shown by A ⪠B. It means all the elements in set A and set B should occur in a single array. In JavaScript, a union set means adding one set element to the other set. Set automatically take care of the un 2 min read How are elements ordered in a Set in JavaScript ? In JavaScript, a new object called Set was introduced in the ES6 version. The Set object is similar to Map but the major difference is that elements in the set are unique and repeated addition of the same type of elements is not possible. The set can store any type of value whether primitive or obje 2 min read How to sort a set in JavaScript ? Sorting a Set based on the values of its elements is a common need in JavaScript. While the built-in sort() function can't be directly applied to a Set, there's a workaround. Let's explore how we can achieve this: Instead of directly sorting the Set, we'll: Convert the Set into an array to make it s 3 min read How to Convert Array to Set in JavaScript? The Set constructor is used to convert the array into set in JavaScript. It is useful to clean the array and remove duplicate elements. Converting Array to Set means transforming the array elements into a Set object.Using Set ConstructorThe Set constructor in JavaScript directly converts an array in 2 min read How to map, reduce and filter a Set element using JavaScript ? The map(), reduce() and filter() are array functions that transform the array according to the applied function and return the updated array. They are used to write simple, short and clean codes for modifying an array instead of using the loops. map() method: It applies a given function on all the e 2 min read Difference between Set.clear() and Set.delete() Methods in JavaScript Both Set.clear() and Set.delete() methods modify the present Set by removing elements. But there are some fundamental differences between these two, which will be discussed in this article. Set.clear() Method: In this method, all the elements in the set get deleted. So, as a result, the set becomes 2 min read Set vs Map in JavaScript In JavaScript, Set and Map are two types of objects that are used for storing data in an ordered manner. Both these data structures are used to store distinct types of data inside the same object. In Maps, the data is stored as a key-value pair whereas in Set data is a single collection of values th 2 min read How to perform intersection of two sets in JavaScript ? An intersection of two sets means the element which occurs in both sets. In mathematical terms, the intersection of two sets is shown by A â© B. It means all the elements which is common in set A and set B should occur in a single array. In javascript, the intersection of two sets means getting the c 2 min read Get the Difference between Two Sets using JavaScript To get the difference between two sets in JavaScript, you need to identify elements present in the first set but not in the second. This involves iterating over the first set and filtering out elements that are also found in the second set, ensuring efficient comparison. We can get the difference be 2 min read Like