how to check if a set is a superset of another set in typescript

To check if one set is a superset of another in TypeScript, you can use the has method and the spread operator to compare the values.

Here's an example:

index.ts
const set1: Set<number> = new Set([1, 2, 3, 4, 5]);
const set2: Set<number> = new Set([3, 4]);

const isSuperset: boolean = [...set2].every(val => set1.has(val));

console.log(isSuperset); // Output: true
205 chars
7 lines

In this example, set1 is the superset and set2 is the subset. We use the every method along with the spread operator to iterate over each value in set2. For each value, we check if it exists in set1 using the has method. If all the values in set2 are found in set1, then isSuperset will be true.

gistlibby LogSnag