Array Intersection vs Difference — Online Tool

Visualize shared elements (intersection) and exclusive elements (difference) between two arrays in a single comparison.

🔒 100% private — runs entirely in your browser

or try sample data

What Are Array Intersection and Difference?

Array intersection and difference are the two fundamental set operations for comparing collections. The intersection of two arrays is the set of elements they share — the overlap. The difference is the set of elements that exist in one array but not the other — the exclusive items. Together, these two operations give you a complete Venn-diagram view of your data: what is shared, what is only on the left, and what is only on the right.

Understanding both operations side by side is essential for many real-world tasks. When comparing database schemas, the intersection tells you which tables exist in both environments, while the difference reveals tables that have been added or dropped. When auditing user permissions, the intersection shows privileges that are common across roles, and the difference highlights what makes each role unique. By seeing intersection and difference together, you avoid the mistake of computing one and assuming the other.

Paste your two arrays below as JSON. Shared elements (intersection) appear in white, elements only in the left array appear in red, and elements only in the right array appear in green. Enable "Ignore array order" for position-independent matching that focuses purely on membership. All computation runs in your browser — no data is transmitted to any server.

Intersection and Difference — Code Examples

JavaScript: Set-based intersection and difference

const arr1 = [1, 2, 3, 4, 5, 6];
const arr2 = [4, 5, 6, 7, 8, 9]; const setA = new Set(arr1);
const setB = new Set(arr2); // Intersection: elements in both
const intersection = arr1.filter(x => setB.has(x));
// [4, 5, 6] // Difference A - B: elements only in arr1
const diffAB = arr1.filter(x => !setB.has(x));
// [1, 2, 3] // Difference B - A: elements only in arr2
const diffBA = arr2.filter(x => !setA.has(x));
// [7, 8, 9] console.log("Shared:", intersection);
console.log("Only in A:", diffAB);
console.log("Only in B:", diffBA);

Converting to Sets gives O(1) lookups per element. The total time complexity is O(n + m) where n and m are the array lengths.

Python: Built-in set methods

list1 = [1, 2, 3, 4, 5, 6]
list2 = [4, 5, 6, 7, 8, 9] set1 = set(list1)
set2 = set(list2) # Intersection
intersection = set1 & set2 # {4, 5, 6}
# or: set1.intersection(set2) # Difference A - B
diff_ab = set1 - set2 # {1, 2, 3}
# or: set1.difference(set2) # Difference B - A
diff_ba = set2 - set1 # {7, 8, 9} # Symmetric difference (union of both differences)
sym_diff = set1 ^ set2 # {1, 2, 3, 7, 8, 9} print(f"Shared: {intersection}")
print(f"Only in A: {diff_ab}")
print(f"Only in B: {diff_ba}")

Python's set operators (&, -, ^, |) map directly to intersection, difference, symmetric difference, and union respectively.

Venn diagram concepts in code

// A complete Venn diagram partition
function vennPartition(arr1, arr2) { const setA = new Set(arr1); const setB = new Set(arr2); return { onlyA: arr1.filter(x => !setB.has(x)), // Left crescent both: arr1.filter(x => setB.has(x)), // Overlap onlyB: arr2.filter(x => !setA.has(x)), // Right crescent };
} const result = vennPartition( ["postgres", "mysql", "redis", "mongodb"], ["redis", "mongodb", "cassandra", "dynamodb"]
); console.log(result.onlyA); // ["postgres", "mysql"]
console.log(result.both); // ["redis", "mongodb"]
console.log(result.onlyB); // ["cassandra", "dynamodb"] // Verify: onlyA + both + onlyB = union
// Verify: onlyA.length + both.length = arr1 (unique elements)

This Venn partition function returns three non-overlapping groups that together form the complete union of both arrays. It is the most informative single operation you can run on two arrays.

Intersection and Difference Gotchas

Difference is not commutative

Array difference depends on direction: A - B is not the same as B - A. If A is [1, 2, 3] and B is [2, 3, 4], then A - B is [1] but B - A is [4]. Always be explicit about which array is the "reference" and which is the "comparison" when discussing differences.

Intersection with duplicates is ambiguous

If array A is [1, 1, 2] and array B is [1, 2, 2], what is the intersection? Set intersection gives [1, 2]. Multiset intersection gives [1, 2] (taking the minimum count). But A.filter(x => B.includes(x)) gives [1, 1, 2] because it does not consume matched elements. Choose your method based on whether duplicates are meaningful.

Deep equality is needed for object arrays

JavaScript's Set and === compare objects by reference, so new Set([{a:1}]).has({a:1}) returns false. For arrays of objects, you must serialize objects (e.g., JSON.stringify) or use a deep equality library to compute intersection and difference correctly. This tool handles deep comparison automatically.

Frequently Asked Questions

What is the difference between intersection and difference?

Intersection gives you elements that appear in both arrays — the overlap, like the center of a Venn diagram. Difference gives you elements in one array that are not in the other — the exclusive items, like the non-overlapping parts of a Venn diagram. Together, they give you a complete picture of how two arrays relate.

How do I compute array intersection in JavaScript?

Convert one array to a Set for efficient lookups, then filter: const intersection = arr1.filter(x => new Set(arr2).has(x)). Create the Set outside the filter for better performance: const setB = new Set(arr2); const intersection = arr1.filter(x => setB.has(x)).

What is a Venn diagram for arrays?

A Venn diagram represents two collections as overlapping circles. The overlapping area shows the intersection (shared elements), the left-only area shows elements exclusive to the first array, and the right-only area shows elements exclusive to the second array. This tool provides the same visual insight using color-coded diff output.

Does intersection preserve duplicate elements?

Standard set-based intersection discards duplicates. If array A has [1, 1, 2] and array B has [1, 2, 2], set intersection gives {1, 2}. For multiset (bag) intersection that respects duplicates, use frequency counting — the result would be [1, 2] with minimum counts from each side.

Can I compute these operations for arrays of objects?

Yes. Paste your arrays of JSON objects and this tool performs deep structural comparison. Objects shared between both arrays are shown as unchanged (intersection), while objects unique to each side are highlighted in red or green (difference). Modified objects that differ in specific fields are also detected.

Is intersection commutative?

Yes, intersection is commutative: A intersect B equals B intersect A. Difference, however, is not commutative: A minus B does not equal B minus A. The symmetric difference (elements in either but not both) is also commutative. This matters when deciding which array goes in which panel.