JavaScript Set Logic
Understanding Set operations and logic
🧠What is Set Logic?
Set logic involves operations like union, intersection, and difference between sets. These operations help you compare and combine sets in useful ways.
// Basic set logic example
const setA = new Set([1, 2, 3]);
const setB = new Set([3, 4, 5]);
console.log("Set A:", setA);
console.log("Set B:", setB);
Set Operations
Union
Combine all elements from both sets
Intersection
Find common elements in both sets
Difference
Elements in one set but not the other
Symmetric Difference
Elements in either set but not both
🔹 Union Operation
Union combines all unique elements from both sets:
const setA = new Set([1, 2, 3]);
const setB = new Set([3, 4, 5]);
// Union: all elements from both sets
const union = new Set([...setA, ...setB]);
console.log("Union:", union);
Output:
🔹 Intersection Operation
Intersection finds elements that exist in both sets:
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5, 6]);
// Intersection: common elements
const intersection = new Set([...setA].filter(x => setB.has(x)));
console.log("Intersection:", intersection);
Output:
🔹 Difference Operation
Difference finds elements in the first set but not in the second:
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5, 6]);
// Difference: elements in A but not in B
const difference = new Set([...setA].filter(x => !setB.has(x)));
console.log("Difference (A - B):", difference);
Output:
🔹 Symmetric Difference
Symmetric difference finds elements in either set but not in both:
const setA = new Set([1, 2, 3, 4]);
const setB = new Set([3, 4, 5, 6]);
// Symmetric difference: elements in A or B but not both
const symDiff = new Set([
...[...setA].filter(x => !setB.has(x)),
...[...setB].filter(x => !setA.has(x))
]);
console.log("Symmetric Difference:", symDiff);
Output:
🔹 Subset and Superset
Check if one set is contained within another:
Set Relationships:
- Subset: All elements of A are in B
- Superset: B contains all elements of A
- Equal: Both sets have the same elements
const setA = new Set([1, 2]);
const setB = new Set([1, 2, 3, 4]);
// Check if A is subset of B
const isSubset = [...setA].every(x => setB.has(x));
console.log("A is subset of B:", isSubset);
// Check if sets are equal
const areEqual = setA.size === setB.size && [...setA].every(x => setB.has(x));
console.log("Sets are equal:", areEqual);
Output:
Sets are equal: false
🔹 Practical Set Logic Functions
Create reusable functions for set operations:
// Utility functions for set operations
function union(setA, setB) {
return new Set([...setA, ...setB]);
}
function intersection(setA, setB) {
return new Set([...setA].filter(x => setB.has(x)));
}
function difference(setA, setB) {
return new Set([...setA].filter(x => !setB.has(x)));
}
// Example usage
const fruits = new Set(['apple', 'banana', 'orange']);
const colors = new Set(['red', 'orange', 'yellow']);
console.log("Union:", union(fruits, colors));
console.log("Intersection:", intersection(fruits, colors));
Output:
Intersection: Set(1) {'orange'}