Logical
import { intersection, except, union, exists, existsAll, existsAny, equals, symmetricDifference, isSubset, isSuperset, isDisjoint,} from 'op-array/logical';intersection(left, right)
Elements present in both arrays. Order follows left. O(n + m).
intersection([1, 2, 3], [2, 3, 4]); // [2, 3]except(source, excluded)
Elements of source not in excluded. O(n + m).
except([1, 2, 3, 4], [2, 4]); // [1, 3]union(left, right)
Combined elements without duplicates, in first-seen order.
union([1, 2, 3], [3, 4, 5]); // [1, 2, 3, 4, 5]exists(source, item)
exists([1, 2, 3], 2); // trueexists([], 2); // falseexistsAll(source, items)
True when every element of items is in source. False if source is
empty.
existsAll([1, 2, 3], [1, 3]); // trueexistsAll([1, 2, 3], [1, 9]); // falseexistsAll([], [1]); // falseexistsAny(source, items)
True when any element of items is in source. False if either
side is empty.
existsAny([1, 2, 3], [4, 2]); // trueexistsAny([1, 2, 3], [4, 5]); // falseexistsAny([], [1]); // falseexistsAny([1], []); // falseequals(left, right)
Set-equality. true when both arrays contain exactly the same distinct
elements regardless of order or duplicates. Not a deep equality
check. Element comparison uses Set (SameValueZero) semantics.
equals([1, 2, 3], [3, 2, 1]); // trueequals([1, 2], [1, 2, 2]); // true (set semantics)equals([1, 2], [1, 3]); // falseequals([], []); // truesymmetricDifference(left, right)
Items present in exactly one of left or right (xor). Left-first then
right ordering; duplicates collapsed via Set. O(n + m).
symmetricDifference([1, 2, 3], [2, 3, 4]); // [1, 4]symmetricDifference([1, 1, 2], [2, 3, 3]); // [1, 3]symmetricDifference([1, 2], [2, 1]); // []symmetricDifference([], []); // []isSubset(left, right)
true when every distinct element of left is in right. Empty left
is vacuously a subset of any array (including []). Element comparison
uses Set (SameValueZero) semantics. O(n + m).
isSubset([1, 2], [1, 2, 3]); // trueisSubset([1, 4], [1, 2, 3]); // falseisSubset([1, 2, 3], [3, 2, 1]); // true (set-equal)isSubset([], [1, 2]); // trueisSubset([1], []); // falseisSuperset(left, right)
true when every distinct element of right is in left, i.e. left
is a (non-strict) superset of right. Any left is vacuously a
superset of [] (including [] itself). Element comparison uses
Set (SameValueZero) semantics. O(n + m).
isSuperset([1, 2, 3], [1, 2]); // trueisSuperset([1, 2, 3], [1, 4]); // falseisSuperset([1, 2, 3], [3, 2, 1]); // true (set-equal)isSuperset([1, 2], []); // trueisSuperset([], [1]); // falseisDisjoint(left, right)
true when left and right share no elements. Either side empty is
vacuously disjoint. Element comparison uses Set (SameValueZero)
semantics. O(n + m).
isDisjoint([1, 2], [3, 4]); // trueisDisjoint([1, 2], [2, 3]); // falseisDisjoint([], [1, 2]); // trueisDisjoint([1, 2], []); // trueisDisjoint([], []); // true