Find Common Elements Between Arrays

Paste two arrays to find their intersection — shared elements highlighted, unique elements flagged.

🔒 100% private — runs entirely in your browser

or try sample data

What is Array Intersection?

Finding common elements between two arrays — also known as computing their intersection — is a fundamental operation in programming and data analysis. Given two collections of data, the intersection tells you which items exist in both. This tool visualizes the overlap by showing shared elements as unchanged and unique elements as additions or removals, giving you a complete picture at a glance.

Array intersection has practical applications across many domains. In access control systems, finding common roles between a user's assigned roles and a resource's required roles determines authorization. In recommendation engines, the overlap between a user's interests and an item's tags drives relevance scoring. In data migration projects, comparing column names or record IDs between source and destination systems reveals what has already been transferred and what remains.

Paste your arrays as JSON (e.g., ["a", "b", "c"]). Enable "Ignore array order" for a true set-intersection comparison where element positions do not matter. The tool supports strings, numbers, booleans, objects, and nested arrays. All processing happens in your browser — no data is sent to any server.

Finding Common Elements — Code Examples

JavaScript — filter + includes for intersection

const arr1 = ["react", "vue", "angular", "svelte"];
const arr2 = ["react", "vue", "solid", "svelte", "qwik"]; // Simple intersection
const common = arr1.filter(item => arr2.includes(item));
console.log(common); // ["react", "vue", "svelte"] // For large arrays, use a Set for O(n) lookup instead of O(n^2)
const set2 = new Set(arr2);
const commonFast = arr1.filter(item => set2.has(item));
console.log(commonFast); // ["react", "vue", "svelte"] // Symmetric difference (elements in either but not both)
const set1 = new Set(arr1);
const onlyIn1 = arr1.filter(x => !set2.has(x));
const onlyIn2 = arr2.filter(x => !set1.has(x));
console.log(onlyIn1); // ["angular"]
console.log(onlyIn2); // ["solid", "qwik"]

Always prefer the Set-based approach for arrays larger than a few dozen elements. The includes() method scans the entire array on each call, making the naive approach O(n*m).

Python — Set intersection with the & operator

arr1 = ["react", "vue", "angular", "svelte"]
arr2 = ["react", "vue", "solid", "svelte", "qwik"] # Set intersection (unordered)
common = set(arr1) & set(arr2)
print(common) # {'react', 'svelte', 'vue'} # Preserve order from arr1
common_ordered = [x for x in arr1 if x in set(arr2)]
print(common_ordered) # ['react', 'vue', 'svelte'] # Using intersection method
common_alt = set(arr1).intersection(arr2)
print(common_alt) # {'react', 'svelte', 'vue'} # Elements unique to each array
only_in_1 = set(arr1) - set(arr2)
only_in_2 = set(arr2) - set(arr1)
print(f"Only in arr1: {only_in_1}") # {'angular'}
print(f"Only in arr2: {only_in_2}") # {'solid', 'qwik'}

Python's set operators (&, -, |, ^) map directly to intersection, difference, union, and symmetric difference. They are the most readable approach for set operations.

Go — Map-based intersection

package main import "fmt" func intersection(a, b []string) []string { set := make(map[string]bool) for _, v := range b { set[v] = true } var result []string for _, v := range a { if set[v] { result = append(result, v) delete(set, v) // prevent duplicates in result } } return result
} func main() { arr1 := []string{"react", "vue", "angular", "svelte"} arr2 := []string{"react", "vue", "solid", "svelte", "qwik"} common := intersection(arr1, arr2) fmt.Println(common) // [react vue svelte]
}

Go has no built-in set type, so a map[string]bool is the standard idiom. The delete call prevents duplicate entries when the same element appears multiple times in the first slice.

Common Elements Gotchas

Duplicate handling differs between set and array intersection

Set intersection discards duplicates: [1, 1, 2] ∩ [1, 2, 2] yields {1, 2}. But if you need to preserve multiplicity (e.g., [1, 1, 2] ∩ [1, 2, 2] should yield [1, 2]), you need a frequency-counting approach rather than a simple set operation. Use a counter or map to track how many times each element appears in both arrays.

Type coercion can produce false matches

In JavaScript, [1, 2].includes("1") returns false (strict equality), but other operations like == would match 1 and "1". In JSON, 1 and "1" are different types. Ensure your data uses consistent types before comparing, or you will miss valid matches or produce spurious ones.

Object identity vs structural equality

Two objects {"id": 1} in different arrays are different references in memory. Languages like JavaScript use reference equality for objects by default, so arr1.filter(x => arr2.includes(x)) will fail to find common objects. You need deep comparison or serialization (e.g., JSON.stringify) to match objects by their content.

Frequently Asked Questions

How do I find common elements between two arrays online?

Paste both arrays into the two panels and click Compare. Elements present in both arrays appear as unchanged (no highlight), while elements unique to either array are color-coded as added (green) or removed (red). Enable "Ignore array order" for positionally-independent matching.

What is the difference between array intersection and array diff?

Array intersection returns only the elements that exist in both arrays — the overlap. Array diff shows the full picture: shared elements, elements added in the second array, and elements removed from the first. This tool displays all three categories simultaneously with color coding.

Can I find common elements in arrays of objects?

Yes. The tool performs deep structural comparison of objects and nested data. Two objects are considered common (matching) if every key-value pair is identical at all nesting levels. This works for arrays of user records, configuration objects, and any JSON structure.

Is my data safe when comparing arrays?

Yes. This tool runs entirely in your browser using client-side JavaScript. Your arrays are never transmitted to any server. You can safely compare production data, API keys (in arrays), and sensitive business data.

Does order matter when finding common elements?

By default, elements are compared by position (index 0 vs index 0, index 1 vs index 1). Enable the "Ignore array order" checkbox to match elements regardless of position, which gives you a true set-intersection-style comparison.

How do I find elements that are NOT common between two arrays?

The tool shows this automatically. After comparing, elements highlighted in red exist only in Array 1, and elements highlighted in green exist only in Array 2. Together, these form the symmetric difference — everything that is not shared.