Python program to find common array elements

Finding common elements in arrays is a frequent task in data processing. Python provides several approaches depending on whether you're working with multi-dimensional arrays or comparing separate arrays.

Finding Common Elements in Multi-Dimensional Arrays

For multi-dimensional arrays, we can use the intersection_update() method with sets to find elements that appear in all sub-arrays ?

Input Output Scenario

Consider a 2D array with multiple sub-arrays ?

arr = [[1, 2, 3, 4], [3, 4, 5, 6], [7, 8, 3, 4], [4, 9, 8, 3], [4, 3, 10, 12]]

# Elements 3 and 4 appear in all sub-arrays
print("Array structure:")
for i, sub_arr in enumerate(arr):
    print(f"Sub-array {i}: {sub_arr}")
Array structure:
Sub-array 0: [1, 2, 3, 4]
Sub-array 1: [3, 4, 5, 6]
Sub-array 2: [7, 8, 3, 4]
Sub-array 3: [4, 9, 8, 3]
Sub-array 4: [4, 3, 10, 12]

Using intersection_update()

The intersection_update() method finds elements common to all sub-arrays ?

def find_common_elements(arr):
    if not arr:
        return []
    
    # Start with the first sub-array as a set
    result = set(arr[0])
    
    # Find intersection with remaining sub-arrays
    for sub_array in arr[1:]:
        result.intersection_update(sub_array)
    
    return list(result)

# Test the function
arr = [[1, 2, 3, 4], [3, 4, 5, 6], [7, 8, 3, 4], [4, 9, 8, 3], [4, 3, 10, 12]]
common = find_common_elements(arr)

if common:
    print(f"Common elements in all sub-arrays: {sorted(common)}")
else:
    print("No common elements found!")
Common elements in all sub-arrays: [3, 4]

Finding Common Elements Between Two Arrays

For comparing two separate arrays, NumPy's intersect1d() function provides an efficient solution ?

Using NumPy intersect1d()

import numpy as np

arr1 = [1, 2, 3, 4, 8]
arr2 = [3, 4, 5, 6, 8]

print("First array:", arr1)
print("Second array:", arr2)

# Convert to NumPy arrays and find intersection
narr1 = np.array(arr1)
narr2 = np.array(arr2)

common_elements = np.intersect1d(narr1, narr2)
print("Common elements:", common_elements)
First array: [1, 2, 3, 4, 8]
Second array: [3, 4, 5, 6, 8]
Common elements: [3 4 8]

Using Set Intersection

For simple cases, Python's built-in set operations work well ?

arr1 = [1, 2, 3, 4, 8]
arr2 = [3, 4, 5, 6, 8]

# Using set intersection
common = list(set(arr1) & set(arr2))
print("Common elements using sets:", sorted(common))

# Alternative syntax
common_alt = list(set(arr1).intersection(set(arr2)))
print("Alternative method:", sorted(common_alt))
Common elements using sets: [3, 4, 8]
Alternative method: [3, 4, 8]

Comparison of Methods

Method Best For Preserves Order Performance
intersection_update() Multi-dimensional arrays No Good
np.intersect1d() Two arrays Yes (sorted) Excellent
Set intersection Simple cases No Good

Conclusion

Use intersection_update() for finding common elements across multiple sub-arrays. For comparing two arrays, np.intersect1d() is most efficient and returns sorted results.

Updated on: 2026-03-27T06:15:33+05:30

351 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements