Sub topic 6
Elements in Exactly One Set: symmetricDifference
This chapter assumes you know how to create a Set and iterate it with for...of, and that you understand spread syntax. You should also have read the earlier chapters on union, intersection, and difference, because symmetricDifference builds on all three.
What symmetricDifference Returns
a.symmetricDifference(b) returns a new Set that holds every element found in exactly one of the two sets. Elements that appear in both sets are left out. Like the other composition methods, it creates a fresh Set and does not change a or b.
const a = new Set([1, 2, 3]);
const b = new Set([3, 4]);
const result = a.symmetricDifference(b);
console.log([...result]); // [1, 2, 4]
console.log([...a]); // [1, 2, 3] (unchanged)
console.log([...b]); // [3, 4] (unchanged)The value 3 is in both sets, so it is dropped. The values 1 and 2 exist only in a, and 4 exists only in b, so all three are kept.
How It Relates to the Other Methods
You can produce the same contents with the methods you already know. Two equivalent formulas exist:
| Formula | Reading |
|---|---|
| `a.union(b).difference(a.intersection(b))` | Everything, minus the overlap |
| `a.difference(b).union(b.difference(a))` | Only-in-a, plus only-in-b |
const a = new Set([1, 2, 3]);
const b = new Set([3, 4]);
const viaOverlap = a.union(b).difference(a.intersection(b));
const viaSides = a.difference(b).union(b.difference(a));
console.log([...viaOverlap]); // [1, 2, 4]
console.log([...viaSides]); // [1, 2, 4]
console.log([...a.symmetricDifference(b)]); // [1, 2, 4]The built-in method is clearer than either formula, and it builds a single result Set where the formulas create two or three temporary ones.
Symmetric in Contents, Not in Order
difference gives different results depending on which set you call it on. symmetricDifference does not: a.symmetricDifference(b) and b.symmetricDifference(a) always contain the same elements. The order can still differ. The method starts from a copy of the receiver, removes the shared elements, and then appends the argument's unique elements.
const a = new Set([1, 2, 3]);
const b = new Set([3, 4]);
console.log([...a.symmetricDifference(b)]); // [1, 2, 4]
console.log([...b.symmetricDifference(a)]); // [4, 1, 2]A Practical Use: Detecting Drift
Symmetric difference answers the question "where do these two collections disagree?" A common case is comparing feature flags between two environments.
const staging = new Set(["darkMode", "newCheckout", "betaSearch"]);
const production = new Set(["darkMode", "newCheckout", "legacyNav"]);
const drift = staging.symmetricDifference(production);
console.log([...drift]); // ["betaSearch", "legacyNav"]
if (drift.size > 0) {
console.log("Only in staging:", [...staging.difference(production)]); // ["betaSearch"]
console.log("Only in production:", [...production.difference(staging)]); // ["legacyNav"]
}symmetricDifference tells you whether a mismatch exists and which elements are involved. It does not tell you which side each element came from. When you need that, call difference in each direction, as the if block does.
Common Mistakes
- Expecting it to mutate the receiver.
a.symmetricDifference(b)on its own line does nothing useful. You have to store or use the returned Set. - Passing an array. The argument must be set-like, so
a.symmetricDifference([3, 4])throws aTypeError. Wrap it first:a.symmetricDifference(new Set([3, 4])). - Confusing it with difference.
a.difference(b)keeps onlya's unique elements.symmetricDifferencekeeps the unique elements from both sides.
Exercise
Given const x = new Set(["red", "green", "blue"]) and const y = new Set(["blue", "yellow", "red"]), what does [...y.symmetricDifference(x)] log, in order?
Show answer
["yellow", "green"]. The result starts as a copy of y (blue, yellow, red). Iterating x, "red" is in y so it is removed, "green" is not in y so it is appended, and "blue" is in y so it is removed. That leaves yellow, then green.
Exercise
Write a function sameElements(a, b) that returns true when two Sets contain exactly the same elements, using symmetricDifference.
Show answer
const sameElements = (a, b) => a.symmetricDifference(b).size === 0; It returns true only when no element appears in just one of the sets.