Subsets And SuperSets
Part of the Logic & Flow section of Coddy's JavaScript journey — lesson 40 of 65.
A set A is a subset of set B if every element of A is also an element of B. Conversely, B is a superset of A if it contains all elements of A.
Two sets are equal if they contain exactly the same elements. For example let's create some tests:
const setA = new Set([1, 2, 3]);
const setB = new Set([1, 2, 3, 4, 5]);
const setC = new Set([1, 2, 3]);
const setD = new Set([6, 7, 8]);Function to check if setA is a subset of setB:
function isSubset(setA, setB) {
return [...setA].every(element => setB.has(element));
}Function to check if setA is a superset of setB:
function isSuperset(setA, setB) {
return isSubset(setB, setA);
}Function to check if two sets are equal:
function areEqual(setA, setB) {
return setA.size === setB.size && isSubset(setA, setB);
}Testing subset relationship:
// A is subset of B?
isSubset(setA, setB) // true
// B is subset of A?
isSubset(setB, setA) // false
// D is subset of B?
isSubset(setD, setB) // falseTesting superset relationship:
// B is superset of A?
isSuperset(setB, setA) // true
// A is superset of B?
isSuperset(setA, setB) // falseTesting equality:
// A equals C?
areEqual(setA, setC) // true
// A equals B?
areEqual(setA, setB) // false
// A equals D?
areEqual(setA, setD) // falseChallenge
EasyCreate a function called analyzeSetRelations that takes two arrays as parameters: arr1 and arr2. The function should convert the arrays to sets and return an object with the following properties:
isSubset: a boolean indicating whetherset1is a subset ofset2isSuperset: a boolean indicating whetherset1is a superset ofset2isEqual: a boolean indicating whetherset1andset2have exactly the same elements
Cheat sheet
A set A is a subset of set B if every element of A is also an element of B. Conversely, B is a superset of A if it contains all elements of A. Two sets are equal if they contain exactly the same elements.
Check if setA is a subset of setB:
function isSubset(setA, setB) {
return [...setA].every(element => setB.has(element));
}Check if setA is a superset of setB:
function isSuperset(setA, setB) {
return isSubset(setB, setA);
}Check if two sets are equal:
function areEqual(setA, setB) {
return setA.size === setB.size && isSubset(setA, setB);
}Try it yourself
function analyzeSetRelations(arr1, arr2) {
let set1 = new Set(arr1);
let set2 = new Set(arr2);
// Use .every() and .has() to check set relations
let isSubset = false; // TODO: check if every element of set1 exists in set2
let isSuperset = false; // TODO: check if every element of set2 exists in set1
let isEqual = false; // TODO: check if both sets have the same size and isSubset is true
return {
isSubset: isSubset,
isSuperset: isSuperset,
isEqual: isEqual
};
}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