xxxxxxxxxx
// perform intersection operation
// elements of set a that are also in set b
function intersection(setA, setB) {
let intersectionSet = new Set();
for (let i of setB) {
if (setA.has(i)) {
intersectionSet.add(i);
}
}
return intersectionSet;
}
// two sets of fruits
let setA = new Set(['apple', 'mango', 'orange']);
let setB = new Set(['grapes', 'apple', 'banana']);
let result = intersection(setA, setB);
console.log(result);
xxxxxxxxxx
function intersection(setA, setB) {
const result = new Set();
for (const item of setA) {
if (setB.has(item)) {
result.add(item);
}
}
return result;
}
// Example usage
const setA = new Set([1, 2, 3, 4, 5]);
const setB = new Set([4, 5, 6, 7, 8]);
const intersectionSet = intersection(setA, setB);
console.log(intersectionSet); // Output: Set { 4, 5 }