array_symmetric_difference

Returns elements that exist in either array but not in both. Similar to set symmetric difference (A △ B) or XOR operation.

{% assign old_tags = "sale,featured,new" | split: "," %}
{% assign new_tags = "featured,bestseller,clearance" | split: "," %}

{% assign changed_tags = old_tags | array_symmetric_difference: new_tags %}
{% log changed_tags | join: ", " %}

Output:

sale, new, bestseller, clearance

Syntax

{{ arrayA | array_symmetric_difference: arrayB }}
Parameter
Description

arrayA

First array

arrayB

Second array

Visual Representation

Array A: [1, 2, 3, 4, 5]
Array B: [3, 4, 5, 6, 7]

Result:  [1, 2, 6, 7]  (elements in A OR B, but NOT both)

This is equivalent to: (A - B) + (B - A)

Examples

Detect all tag changes:

Find inventory discrepancies:

Compare collection membership:

Sync differences between systems:

Detect permission changes:

Performance

Uses Set-based lookup for O(n + m) time complexity instead of O(n × m + m × n).

Array A Size
Array B Size
Operations

1,000

1,000

~4,000

10,000

10,000

~40,000

100,000

100,000

~400,000

Notes

  • Both inputs must be arrays; throws error otherwise

  • Result contains elements from arrayA first, then elements from arrayB

  • Returns empty array if both arrays contain exactly the same elements

  • Useful for detecting any kind of change between two states

  • Comparison uses strict equality (===)

  • See also: array_difference, array_intersection, array_equal

Last updated