Skip to curriculum
Jump to the editor

4.8. Difference, intersection and union (plain, by key, with a comparator)

Medium
Topics

This question is in

4. Array utilities
20–35 min

A 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

JAVASCRIPT
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 array

options is one of:

  • missing or {}: compare items as a Set does (NaN matches NaN, 0 matches -0, objects match only themselves);
  • { by }: compare keys. by is a function, called as by(item), or a property name such as "id";
  • { equals }: compare with your function. equals(item, other) returns a truthy value when the two match. item is the one being checked; other is an item of b (for difference and intersection) or an item already kept.

Requirements

  1. difference(a, b) keeps the items of a that have no match in b, in a's order. Repeats in a are all kept.
  2. intersection(a, b) keeps the items of a that have a match in b, in a's order, and keeps only the first of items that match each other.
  3. union(a, b) keeps the items of a and then those of b, skipping any item that matches one already kept.
  4. Without options, items are compared as a Set compares them; no type conversion (1 and "1" differ).
  5. With by, items match when their keys match (keys compared as a Set compares them). A function and a property name both work.
  6. With equals, items match when equals(item, other) returns a truthy value, with the arguments in the order above.
  7. The results hold the original items, never keys; for intersection they are items of a.
  8. Empty arrays work in either place. The inputs are not changed, and every result is a new array.
  9. Without options and with by, 50,000 items against 50,000 items take well under a second.

Examples

JAVASCRIPT
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]
JAVASCRIPT
const has = [{ id: "read" }, { id: "write" }];
const needs = [{ id: "write" }, { id: "admin" }];
difference(needs, has, { by: "id" });   // [{ id: "admin" }]
JAVASCRIPT
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 kept

Environment

  • Plain JavaScript in a browser page; the tests call the globals difference, intersection and union from setOps.js.
  • Arrays have no holes. Only one of by and equals is passed at a time.
  • With equals there is no key to look up, so comparing every pair is expected; the speed requirement does not apply.

Hints

  1. 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?
  2. 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
Back to the question
Loading the editor…