Math - Difference Of Sets
Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 38 of 65.
The difference of two sets A and B (often written as A - B) is a new set that contains elements that are in A but not in B. This operation is not commutative, meaning A - B is not necessarily the same as B - A.
For example:
const setA = new Set([1, 2, 3, 4, 5]);
const setB = new Set([3, 4, 5, 6, 7]);
// A - B: elements in A but not in B
const differenceAB = new Set([...setA].filter(x => !setB.has(x)));
console.log('A - B:', differenceAB); // Set { 1, 2 }
// B - A: elements in B but not in A
const differenceBA = new Set([...setB].filter(x => !setA.has(x)));
console.log('B - A:', differenceBA); // Set { 6, 7 }Challenge
EasyCreate a function called setDifference that takes two arrays as parameters: arr1 and arr2. The function should convert the arrays to sets. Create a new Set that contains elements that are in set1 but not in set2, convert it an array and return it
Cheat sheet
The difference of two sets A and B (A - B) creates a new set containing elements that are in A but not in B. This operation is not commutative (A - B ≠ B - A).
To find the difference between sets, convert arrays to sets and use set operations to find elements in the first set but not in the second. For example:
const setA = new Set([1, 2, 3, 4, 5]);
const setB = new Set([3, 4, 5, 6, 7]);
// A - B: elements in A but not in B
const differenceAB = new Set([...setA].filter(x => !setB.has(x)));
console.log('A - B:', differenceAB); // Set { 1, 2 }
// B - A: elements in B but not in A
const differenceBA = new Set([...setB].filter(x => !setA.has(x)));
console.log('B - A:', differenceBA); // Set { 6, 7 }Try it yourself
function setDifference(arr1, arr2) {
const set1 = new Set(arr1)
const set2 = new Set(arr2)
const differenceSet = new Set();
// Write your code here
return Array.from(differenceSet);
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Strings In Depth
String FundamentalsIterate Over StringsTemplate LiteralsString MethodsRecap - String Weaver4JSON Part 2
Iterate Over JSONNested JSONJSON Optional ChainingShallow And Deep CopyRecap - Bicycle ShopRecap - Solar System10Manage Festival System
Project OverviewAdd Movies & Venues2Multi-dimensional Arrays
2D Arrays BasicsAccessing 2D Array ElementsNested Loops with 2D ArraysRecap - 2D ArraysMatrix Addition & SubstractionJagged Arrays3D Arrays And BeyondCommon 2D Array PatternsRecap - All About Arrays5Sets Part 1
What Is A Set?Iterating Over SetsAdding An ElementRemoving An ElementChecking If An Element ExistsSize And Is EmptyCopy And ClearRecap - Basic Of Sets8Arrays Interesting Topics
Array DestructuringSpread Syntax in ArraysSparse ArraysRecap - Arrays Workshop3JSON Part 1
What is a JSON?Check If Key ExistsObject MethodsThe Spread Operator Part 1The Spread Operator Part 2Remove KeysRecap - JSON Manipulate Keys6Sets Part 2
Math - Union Of SetsMath - Intersection Of SetsMath - Difference Of SetsMath - Symmetric DifferenceSubsets And SuperSetsRecap - Group Friends