Skip to the solution
EasyFrontend5 min read

How do the new Set methods union(), intersection(), difference() and symmetricDifference() work?

435 views

The problem

Question presented to candidate: "Before these Set methods existed, you'd write manual loops (or filter/some combinations) to find the overlap between two Sets. What do union, intersection, difference, and symmetricDifference actually give you β€” and do any of them mutate the original Sets?"

What a strong answer should cover:

  • πŸ“Œ Interview term: Set.prototype.union(other) β€” returns a NEW Set containing every element from BOTH sets, real set-theory union β€” verified directly with two overlapping real Sets.
  • πŸ“Œ Interview term: Set.prototype.intersection(other) β€” returns a new Set containing only elements present in BOTH sets.
  • πŸ“Œ Interview term: Set.prototype.difference(other) β€” returns a new Set with elements in the FIRST set but NOT the second β€” verified directly to be genuinely asymmetric: a.difference(b) and b.difference(a) produce real, different results.
  • πŸ“Œ Interview term: Set.prototype.symmetricDifference(other) β€” returns a new Set with elements in EITHER set but not both β€” verified directly.
  • πŸ“Œ Interview term: the real, direct answer to the prompt's mutation question β€” verified directly: none of these methods mutate the original Set at all β€” calling .union() genuinely left the original Set's .size completely unchanged, matching the identical non-mutating pattern this bank's own array-methods coverage establishes for map/filter/slice.
  • A precise answer names that these methods genuinely work on any "Set-like" object (anything with a real .size, .has(), and .keys()), not strictly a real Set instance β€” verified directly β€” plus the three real boolean-returning bonus methods that shipped alongside them: isSubsetOf, isSupersetOf, isDisjointFrom.

Clarifying questions expected:

  • None β€” this is a definitional/technical question; directly answering the prompt's own mutation question (none of them mutate) is the strong signal.

Code / implementation expected: Yes β€” real results for all four operations on the same two overlapping Sets, plus the direct non-mutation proof, is the clearest demonstration.

  • set
  • es2024
  • collection

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

5 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal results for union/intersection/difference/symmetricDifference on two overlapping Sets, plus direct proof none of them mutate the original

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 27 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextEasy

What is the difference between synchronous and asynchronous code?

Continue