Find Common Elements in Two Arrays in Python (original) (raw)

Last Updated : 23 Jul, 2025

Given two arrays **arr1[] and **arr2[], the task is to find all the common elements among them.

**Examples:

**Input: arr1[] = {1, 2, 3, 4, 5}, arr2[] = {3, 4, 5, 6, 7}
**Output: 3 4 5
**Explanation: 3, 4 and 5 are common to both the arrays.

**Input: arr1: {2, 4, 0, 5, 8}, arr2: {0, 1, 2, 3, 4}
**Output: 0 2 4
**Explanation: 0, 2 and 4 are common to both the arrays.

Find Common Elements in Two Arrays using Brute Force:

This is a brute force approach, simply traverse in the first array, for every element of the first array traverse in the second array to find whether it exists there or not, if true then check it in the result array (to avoid repetition), after that if we found that this element is not present in result array then print it and store it in the result array.

**Step-by-step approach:

Below is the implementation of the above approach:

Python `

Python Program for find commom element using two array

import array

arr1 = array.array('i', [1, 2, 3, 4, 5]) arr2 = array.array('i', [3, 4, 5, 6, 7]) result = array.array('i')

print("Common elements are:", end=" ")

To traverse array1.

for i in range(len(arr1)): # To traverse array2. for j in range(len(arr2)): # To match elements of array1 with elements of array2. if arr1[i] == arr2[j]: # Check whether the found element is already present in the result array or not. if arr1[i] not in result: result.append(arr1[i]) print(arr1[i], end=" ") break

`

Output

Common elements are: 3 4 5

**Time Complexity: O(n * m), where **n is the number of elements in array **arr1[] and **m is the number of elements in **arr2[].
**Auxiliary Space: O(k), where **k is the number of common elements between the two arrays.

Find Common Elements in Two Arrays Using List Comprehension:

To find the common elements in two arrays in python, we have used list comprehension. For each element **X in arr1, we check if X is present in arr2 and store it in a list.

**Step-by-step approach:

Below is the implementation of the above approach:

Python `

Python Program for the above approach

from array import array def find_common_elements(arr1, arr2): common_elements = array('i', [x for x in arr1 if x in arr2]) return list(common_elements)

Driver Code

arr1 = array('i', [1, 2, 3, 4, 5]) arr2 = array('i', [3, 4, 5, 6, 7]) common_elements = find_common_elements(arr1, arr2) print("Common elements:", common_elements)

`

Output

Common elements: [3, 4, 5]

**Time Complexity : O(n*m)
**Auxiliary Space: O(k), where k is the number of common elements between the two arrays.

Find Common Elements in Two Arrays using **Sorting:

To find the common elements in two arrays in Python, we have to first sort the arrays, then just iterate in the sorted arrays to find the common elements between those arrays.

**Step-by-step approach:

Below is the implementation of the above approach:

Python `

import array

def find_common_elements(arr1, arr2): # Convert arrays to lists for sorting arr1_list = list(arr1) arr2_list = list(arr2)

# Sort both lists in non-decreasing order
arr1_list.sort()
arr2_list.sort()

# Initialize pointers
pointer1 = 0
pointer2 = 0

# Initialize an empty array to store common elements
common_elements = array.array('i')

# Iterate through both arrays simultaneously
while pointer1 < len(arr1_list) and pointer2 < len(arr2_list):
    # If the elements pointed by pointer1 and pointer2 are equal
    if arr1_list[pointer1] == arr2_list[pointer2]:
        # Add the element to the result array
        common_elements.append(arr1_list[pointer1])
        # Move both pointers forward
        pointer1 += 1
        pointer2 += 1
    # If the element in arr1 pointed by pointer1 is less than the element in arr2 pointed by pointer2
    elif arr1_list[pointer1] < arr2_list[pointer2]:
        # Move pointer1 forward
        pointer1 += 1
    # If the element in arr2 pointed by pointer2 is less than the element in arr1 pointed by pointer1
    else:
        # Move pointer2 forward
        pointer2 += 1

return common_elements

Test the function with example arrays

arr1 = array.array('i', [1, 2, 3, 4, 5]) arr2 = array.array('i', [3, 4, 5, 6, 7]) common_elements = find_common_elements(arr1, arr2) print "Common elements:", for element in common_elements: print element,

`

Output

Common elements: 3 4 5

**Time Complexity: O(N log(N)+ M log(M)), where **N is the size of the first array and **M is the size of the second array.
**Auxilliary Space: O(N+M)

Find Common Elements in Two Arrays Using Sets:

To find the common elements in two arrays in Python, in this approach we will use sets. Initially, we convert both the arrays **arr1 and **arr2 to sets **set1 and **set2. Then, we use the intersection method of set to find the common elements in both the sets. Finally, we **return the common elements as a **list.

**Step-by-step approach:

Below is the implementation of the above approach:

Python `

Python Program for the above approach

from array import array def find_common_elements(arr1, arr2): # Convert arrays to sets for faster lookup set1 = set(arr1) set2 = set(arr2) # Find intersection of the sets (common elements) common_elements = set1.intersection(set2) return list(common_elements)

Driver Code

arr1 = array('i', [1, 2, 3, 4, 5]) arr2 = array('i', [3, 4, 5, 6, 7]) common_elements = find_common_elements(arr1, arr2) print("Common elements:", common_elements)

`

Output

Common elements: [3, 4, 5]

**Time Complexity : O(n+m)
**Auxiliary Space: O((n+m+k), where k is the number of common elements between the two arrays.

Find Common Elements in Two Arrays Using Hash Maps

In this approach, we can utilize hash maps to efficiently find the common elements between the two arrays. We'll create a hash map to the store the frequency of the each element in the first array and then iterate through the second array to the check if each element exists in the hash map. If an element exists in the hash map and its frequency is greater than 0 and we'll add it to the list of common elements and decrease its frequency in the hash map.

**Here's the step-by-step algorithm:

Example code:

Python `

def GFG(arr1, arr2): # Create a hash map to store the frequency of the each element in arr1 frequency_map = {} for num in arr1: frequency_map[num] = frequency_map.get(num, 0) + 1 # Initialize a list to store the common elements common_elements = [] # Iterate through arr2 to the find common elements for num in arr2: if num in frequency_map and frequency_map[num] > 0: common_elements.append(num) frequency_map[num] -= 1

return common_elements

Test the function with the example arrays

arr1 = [1, 2, 3, 4, 5] arr2 = [3, 4, 5, 6, 7] common_elements = GFG(arr1, arr2) print("Common elements:", common_elements)

`

output :

Common elements: 3, 4, 5

Time Complexity : O(n+m)

Auxiliary Space: O(N)