Learn how to implement union ,intersection , difference , symmetricDifference in JavaScript.

The Set object lets you store unique values of any type, whether primitive values or object references.
Example
var array = [1,2,3,4,4,5];
var set = new Set(array);
set; // Set(5) {1, 2, 3, 4, 5} duplicates are removed
The detailed introduction to Set is provided here.
Set operations that we are going to implement
- Union → returns a new
Setwhich has all the elements from both set. - Intersection→ returns a new
Setwhich has common elements in both set. - Difference →
Set A—Set Bwill return elements fromSet Awhich are not inSet B. - Symmetric Difference → Returns a new set , which is created from elements of
Set A, which are not present inSet Band elements ofSet Bwhich are not present inSet A.
1. Union
The union method returns all elements from SetA and SetB ,
function union(setA, setB) {
return new Set([...setA, ...setB]);
}
var setA = new Set([1,2,3,4,5]);
var setB = new Set([3,4,5,6]);
union(setA, setB); Set(6) {1, 2, 3, 4, 5, 6}
2. Intersection
The Intersection operation returns a new Set which has common elements in both set. We can find that by
- Loop through
setA - If element of
setApresent insetB, push to a new set
We can check if a Set has an element using has method in Set object.
function intersect(setA, setB) {
var commonElements = new Set();
for (var elem of setB) {
if (setA.has(elem)) {commonElements.add(elem);
}
}commonElements;
return
}
var setA = new Set([1,2,3,4,5]);
var setB = new Set([3,4,5,6]);
intersect(setA, setB); // Set(3) {3, 4, 5}
3 . Difference
Set A — Set B will return elements from Set A which are not in Set B , if we perform Set B — Set A then the function will return elements from Set B which are not present in Set A . We can achieve this by
- Loop through
Set Band delete all elements ofSet BinSet Ausingdeletemethod in Set object
function difference(setA, setB) {
var diff = new Set(setA);
for (var elem of setB) {
diff.delete(elem);
}
return diff;
}
var setA = new Set([1,2,3,4,5]);
var setB = new Set([3,4,5,6]);
// SET A - SET B
difference(setA, setB); // Set(3) {1,2}
// SET B- SET A
difference(setB, setA); // Set(3) {6}
4. Symmetric Difference
This method returns a new set , which is created from elements of Set A , which are not present in Set B and elements of Set B which are not present in Set A . We can achieve this by
- Create a new set
Result SetfromSet A
For each Element of Set B
- If the element of
Set Bpresent inResult Setthen remove it . - If the element is not present then add to the
Result Set
function symmetricDifference(setA, setB) {
var resultSet = new Set(setA);
for (var elem of setB) {
if (resultSet.has(elem)) {
resultSet.delete(elem);
} else {
resultSet.add(elem);
}
}
return resultSet;
}
var setA = new Set([1,2,3,4,5]);
var setB = new Set([3,4,5,6]);
symmetricDifference(setA, setB); // Set(3) {1, 2, 6}
5. isSuperSet
This is a util function which will check of all elements on subset present in Set . We can achieve this by
For each element of subset
- if any element is not present in
setreturn false.
function isSuperset(set, subset) {
for (var elem of subset) {
if (!set.has(elem)) {
return false;
}
}
return true;
}
var set = new Set([1,2,3,4]);
var subset = new Set([3,4]);
isSuperset(set, subset); // true
subset = new Set([3,4,6]);
isSuperset(set, subset); // false
Thanks for Reading 📖 . Hope you like this. If you found any typo or mistake send me a private note 📝 thanks 🙏 😊 .
Follow me JavaScript Jeep🚙💨 .
Please make a donation here. 80% of your donation is donated to someone needs food 🥘. Thanks in advance.