JavaScript Program to Illustrate Different Set Operations Last Updated : 18 Sep, 2023 Comments Improve Suggest changes Like Article Like Report This article illustrates the various operations that can be applied in a set. There are various operations that can be applied on a set that are listed below: Union of setsIntersection of setsSet differenceSet Subset OperationUnion of setsA 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. Example: JavaScript function showUnion(sA, sB) { const union = new Set(sA); for (const num of sB) { union.add(num); } return union; } const s1 = new Set(['1', '6', '8']); const s2 = new Set(['2', '3', '4']); console.log(showUnion(s1, s2)); OutputSet(6) { '1', '6', '8', '2', '3', '4' } Intersection of setsAn intersection of two sets means the element that 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. Example: JavaScript function getIntersection(set1, set2) { const ans = new Set(); for (let i of set2) { if (set1.has(i)) { ans.add(i); } } return ans; } const set1 = new Set([1, 2, 3, 8, 11]); const set2 = new Set([1, 2, 5, 8]); const result = getIntersection(set1, set2); console.log(result); OutputSet(3) { 1, 2, 8 } Set differenceSet difference means that the array we subtract should contain all the unique element which is not present in the second array. Example: JavaScript let A = [7, 2, 6, 4, 5]; let B = [1, 6, 4, 9]; const set1 = new Set(A); const set2 = new Set(B); const difference = new Set(); set1.forEach(element => { if (!set2.has(element)) { difference.add(element); } }); console.log([...difference]); Output[ 7, 2, 5 ] Set Subset OperationThe set subset operation returns true if all the elements of setB are in setA. Example: JavaScript function subSet(val1, val2) { // Get an iterator for val2 const iterator = val2.values(); let nextVal = iterator.next(); while (!nextVal.done) { if (!val1.has(nextVal.value)) { return false; } nextVal = iterator.next(); } return true; } // Two sets const val1 = new Set(['HTML', 'CSS', 'Javascript']); const val2 = new Set(['HTML', 'CSS']); const result = subSet(val1, val2); console.log(result); Outputtrue Comment More infoAdvertise with us Next Article JavaScript Program to Illustrate Different Set Operations G geekwriter2024 Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League JavaScript-DSA JavaScript-Program Geeks Premier League 2023 +2 More Similar Reads JavaScript Set Coding Practice Problems Sets are an important data structure in JavaScript that store unique values of any type, whether primitive or object references. A Set is useful for scenarios where you want to eliminate duplicate values or check for membership efficiently. Sets are unordered collections, meaning the elements are no 2 min read JavaScript set Interview Questions A Set in JavaScript is a collection of unique values that maintains insertion order and removes duplicates automatically. It supports efficient operations like adding, deleting, and checking values, making it ideal for tasks involving uniqueness or deduplication. Unlike arrays, Set does not allow du 3 min read JavaScript Set Programming Examples In JavaScript, the Set is a built-in collection that allows you to store unique values of any type, Set can store any type of value whether primitive or objects. It provides methods for adding, removing, and querying elements in an efficient way.JavaScriptlet s = new Set(["a", "b", "c"]); console.lo 1 min read Creating and Populating a TreeSet in Java In Java, TreeSet is a pre-defined class that can be used to implement the Set interface and it is a part of the Java collection framework TreeSet is a NavigableSet implementation based on the TreeMap. TreeSet follows the natural order which means elements can be stored in sorted order and it cannot 2 min read Merge Two Sets in Java The set interface is present in java.util package and extends the Collection interface is an unordered collection of objects in which duplicate values cannot be stored. It is an interface that implements the mathematical set. This interface contains the methods inherited from the Collection interfac 12 min read Difference Between List and Set in Java The List interface allows storing the ordered collection. It is a child interface of Collection. It is an ordered collection of objects in which duplicate values are allowed to store. List preserves the insertion order, it allows positional access and insertion of elements. Declaration: public abstr 2 min read Practice Questions on Operations on Sets When studying set theory, having a good understanding of the various operations on sets is crucial and an effective way to deepen this understanding is by working through practice questions on operations on sets. These Practice Questions on Operations on Sets will help to reinforce the fundamental c 5 min read ConcurrentSkipListSet tailSet() method in Java with Examples The tailSet() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java where the elements which are equal to or greater than the specified element are returned. The syntax of the function gives a clear understanding of the specified element followed by the examples.Syntax 2 min read ConcurrentSkipListSet subSet() method in Java with Examples The subSet() method of java.util.concurrent.ConcurrentSkipListSet is an in-built function in Java where the elements are returned in a range defined by this method. The syntax of the function gives a clear understanding of the specified element followed by the examples.Syntax: subSet(E fromElement, 2 min read How to Create Subsets of a HashSet based on Specific Criteria in Java? In Java, HashSet provides a dynamic collection of unique elements. Sometimes, we may need to create subsets of a HashSet based on specific criteria. In this article, we will learn how to create subsets of a HashSet based on Specific Criteria in Java. Approach Create Subsets of a HashSet based on Spe 2 min read Like