Skip to content

symmetricDifference

Returns the symmetric difference between two arrays: items present in exactly one of the two arrays (in either, but not both). null and undefined are treated as empty arrays.

Available since v3.0.0

import { symmetricDifference } from '@helpers4/array';
// or, from the all-in-one package (same code, one install):
import { symmetricDifference } from 'helpers4/array';
symmetricDifference<T>(array1: readonly T[] | null | undefined, array2: readonly T[] | null | undefined): T[]
ParameterTypeDescription
array1readonly T[] | null | undefinedFirst array
array2readonly T[] | null | undefinedSecond array

T[] — Items unique to array1 (in original order), followed by items unique to array2

Complements difference()/intersection() with the “in either, not both” set operation.

symmetricDifference([1, 2, 3], [2, 3, 4])
// => [1, 4]

Identical arrays have no symmetric difference

Section titled “Identical arrays have no symmetric difference”

Items shared by both arrays are excluded entirely.

symmetricDifference(['a', 'b'], ['a', 'b'])
// => []

View source on GitHub