Skip to main content

xor()

xor<T>(arrays): T[]

Creates an array of unique values that is the symmetric difference of the given arrays.

note

Returns values from all arrays that appear in exactly one array.


Type Parameters​

T: T​

The type of elements in the arrays.


Parameters​

arrays: readonly (readonly T[])[]​

The arrays to inspect.


Returns: T[]​

A new array of filtered values.


Since​

2.0.0


Performance​

O(n) β€” uses Map for counting occurrences across arrays.


Also known as​

symmetricDifference (Remeda, Ramda) · xor (Lodash, es-toolkit) · ❌ (Radashi, Effect, Modern Dash, Antfu)


Example​

xor([[2, 1], [2, 3]]);
// => [1, 3]

xor([[1, 2, 3], [3, 4, 5], [5, 6]]);
// => [1, 2, 4, 6]

How it works?​

Symmetric difference β€” returns values that appear in exactly one array. Values present in multiple arrays are excluded.

Count occurrences​

xor vs difference vs intersection​

FunctionReturns
difference(a, b)In A but not B
intersection(a, b)In both A and B
xor([a, b])In A or B, but not both

Use Cases​

Find items unique to each array πŸ“Œβ€‹

Get elements that exist in one array but not both (symmetric difference). Perfect for comparing lists and finding discrepancies.

const listA = ["apple", "banana", "cherry"];
const listB = ["banana", "cherry", "date"];

const uniqueToEach = xor([listA, listB]);
// => ["apple", "date"]

Detect changes between snapshots​

Find what was added or removed between two states. Useful for tracking modifications or generating changelogs.

const before = ["user1", "user2", "user3"];
const after = ["user2", "user3", "user4"];

const changes = xor([before, after]);
// => ["user1", "user4"] (user1 removed, user4 added)

Compare feature sets​

Find features unique to each plan or version. Essential for plan comparison or feature matrices.

const basicPlan = ["storage", "email", "support"];
const proPlan = ["storage", "email", "analytics", "api"];

const differences = xor([basicPlan, proPlan]);
// => ["support", "analytics", "api"]

Highlight config drift between environments​

Detect configuration differences between staging and production. Critical for CI/CD pipelines and deployment validation.

const stagingFlags = ["dark-mode", "new-checkout", "beta-search", "ai-chat"];
const productionFlags = ["dark-mode", "new-checkout", "legacy-nav"];

const drift = xor([stagingFlags, productionFlags]);
// => ["beta-search", "ai-chat", "legacy-nav"]

if (drift.length > 0) {
console.warn(`Config drift detected: ${drift.join(", ")}`);
}

Invert selection in a data table​

Invert the current selection by XOR-ing with all available items. Essential for "Invert Selection" actions in data tables and file managers.

const allItemIds = ["row-1", "row-2", "row-3", "row-4", "row-5"];
const currentSelection = ["row-1", "row-3"];

// Symmetric difference with the full list = items not currently selected
const inverted = xor([allItemIds, currentSelection]);
// => ["row-2", "row-4", "row-5"]

updateSelection(inverted);

Toggle multiple selections at once​

Apply a bulk toggle operation on a selection list. Perfect for "select all in category" or "invert selection" actions.

const currentSelection = ["item-1", "item-3", "item-5"];
const categoryItems = ["item-3", "item-4", "item-5", "item-6"];

// XOR toggles: removes item-3 and item-5 (already selected), adds item-4 and item-6
const newSelection = xor([currentSelection, categoryItems]);
// => ["item-1", "item-4", "item-6"]