cf.completefrontend

Write the codeMedium

Write union, intersection, and difference for two Sets.

Each function takes two Sets, a and b, and returns a new Set. Neither input may change.

union(a, b) has every value that's in a, in b, or in both.

intersection(a, b) has only the values that are in both a and b.

difference(a, b) has the values in a that aren't in b.

Keep the values in the order they appear in a. In a union, the values that are only in b come after them, in the order they appear in b.

Write this
function union(a, b) {}
function intersection(a, b) {}
function difference(a, b) {}
 
// const a = new Set([1, 2, 3]);
// const b = new Set([3, 4]);
// union(a, b);        // Set {1, 2, 3, 4}
// intersection(a, b); // Set {3}
// difference(a, b);   // Set {1, 2}

The answer

Write it yourself first. Then compare it with a reference solution and the checks it passes.

Checking your sign-in…