4.8. Difference, intersection and union (plain, by key, with a comparator)
Topics
This question is in
4. Array utilitiesA settings page compares the permissions a user has with the ones a role needs: the missing ones are a difference. A product filter shows only items that are in both the "on sale" and the "in stock" lists: an intersection. A feed merges two pages of results without showing a post twice: a union. Real data is usually objects, so "the same item" often means "the same id", or something only your own function can decide, such as two dates on the same day.
Write difference, intersection and union in setOps.js. Each takes two arrays and an optional options object that says how items are compared.
Signature
function difference(a, b, options) // returns a new array
function intersection(a, b, options) // returns a new array
function union(a, b, options) // returns a new arrayoptions is one of:
- missing or
{}: compare items as aSetdoes (NaNmatchesNaN,0matches-0, objects match only themselves); { by }: compare keys.byis a function, called asby(item), or a property name such as"id";{ equals }: compare with your function.equals(item, other)returns a truthy value when the two match.itemis the one being checked;otheris an item ofb(fordifferenceandintersection) or an item already kept.
Requirements
difference(a, b)keeps the items ofathat have no match inb, ina's order. Repeats inaare all kept.intersection(a, b)keeps the items ofathat have a match inb, ina's order, and keeps only the first of items that match each other.union(a, b)keeps the items ofaand then those ofb, skipping any item that matches one already kept.- Without options, items are compared as a
Setcompares them; no type conversion (1and"1"differ). - With
by, items match when their keys match (keys compared as aSetcompares them). A function and a property name both work. - With
equals, items match whenequals(item, other)returns a truthy value, with the arguments in the order above. - The results hold the original items, never keys; for
intersectionthey are items ofa. - Empty arrays work in either place. The inputs are not changed, and every result is a new array.
- Without options and with
by, 50,000 items against 50,000 items take well under a second.
Examples
difference([1, 3, 1, 2], [2]); // [1, 3, 1]: repeats in a are kept
intersection([3, 1, 3, 2], [3, 2]); // [3, 2]
union([2, 1], [1, 3, 2]); // [2, 1, 3]const has = [{ id: "read" }, { id: "write" }];
const needs = [{ id: "write" }, { id: "admin" }];
difference(needs, has, { by: "id" }); // [{ id: "admin" }]const sameDay = (x, y) => x.toDateString() === y.toDateString();
union([new Date(2026, 0, 1, 9)], [new Date(2026, 0, 1, 18), new Date(2026, 0, 2)], { equals: sameDay });
// [Jan 1 09:00, Jan 2]: the 18:00 date matches one already keptEnvironment
- Plain JavaScript in a browser page; the tests call the globals
difference,intersectionandunionfromsetOps.js. - Arrays have no holes. Only one of
byandequalsis passed at a time. - With
equalsthere is no key to look up, so comparing every pair is expected; the speed requirement does not apply.
Hints
- All three functions ask the same question: "is there a match for this item in that group?" Can one helper answer it for all three ways of comparing?
- With a key, what makes "is this key in that group?" fast?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it