Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Program to Illustrate Different Set Operations

JavaScript Program to Illustrate Different Set Operations

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 sets
  • Intersection of sets
  • Set difference
  • Set Subset Operation

Union of sets

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.

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));


Output

Set(6) { '1', '6', '8', '2', '3', '4' }

Intersection of sets

An 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);


Output

Set(3) { 1, 2, 8 }

Set difference

Set 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 Operation

The 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);


Output

true
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments