docs/reference/compat/array/xor.md
::: warning Use xor from es-toolkit
This xor function operates slowly due to handling of null or undefined, complex duplicate calculation logic, etc.
Instead, use the faster and more modern xor from es-toolkit.
:::
Creates a new array of elements that exist in exactly one of the multiple arrays.
const result = xor(...arrays);
xor(...arrays)Computes the symmetric difference of multiple arrays. In other words, returns elements that exist in exactly one of the given arrays. This is useful when you want to find non-overlapping unique elements when comparing two or more arrays.
import { xor } from 'es-toolkit/compat';
// Symmetric difference of two arrays
xor([1, 2, 3, 4], [3, 4, 5, 6]);
// Returns: [1, 2, 5, 6]
// Symmetric difference of three arrays
xor([1, 2], [2, 3], [4, 5]);
// Returns: [1, 3, 4, 5]
// String arrays
xor(['a', 'b'], ['b', 'c']);
// Returns: ['a', 'c']
// Providing only one array
xor([1, 2, 3]);
// Returns: [1, 2, 3]
null, undefined, or empty arrays are ignored and only valid arrays are processed.
import { xor } from 'es-toolkit/compat';
xor([1, 2], null, [2, 3]);
// Returns: [1, 3]
xor([], [1, 2], [2, 3]);
// Returns: [1, 3]
...arrays (Array<ArrayLike<T> | null | undefined>): The arrays to compute the symmetric difference from. null or undefined are ignored.(T[]): Returns a new array of elements that exist in exactly one of the arrays.