# Python Program to Find Common Elements in Two Arrays

An array is a data structure consisting of a collection of elements of same data type, and each element is identified by an index.

[2, 4, 0, 5, 8]
0  1  2  3  4


The integers 2, 4, 0, 5, 8 are the array elements and 0, 1, 2, 3, 4 are the respective index values of the array elements.

In The article below, we will discuss the python program to find common elements between two arrays.

## Input Output Scenarios

Assuming we have two arrays A and B. And the resultant array has the common elements between two arrays.

Input arrays:
A = [1, 2, 3, 4, 5]
B = [5, 2, 6, 3, 9]

Output array:
[2, 3, 5]


The elements 2, 3, 5 are the common elements between the two input arrays.

Python does not have a native array data structure to represent arrays. Instead, we can use List as an array. Let’s see the multiple ways to find common elements.

## Using List Comprehension

In python, List comprehension provides the shortest syntax to build a list without initializing an empty list to append values for each iteration.

### Example

In the below example we will define the common elements using the list comprehension.

a = [1, 2, 3, 4, 5]
b = [5, 2, 6, 3, 9]

result = [i for i in a if i in b]

print("The common elements are:", result)


### Output

The common elements are: [2, 3, 5]


Initially, Iterated all elements of array a using the for loop and verified for the each element if it is present in array bor not. And finally, all common elements are stored in the resultant array.

## Using Sets

Finding the common elements in two arrays is very similar to finding the intersection of two sets. And by using Python Sets data structure and properties we can easily identify the common elements in two arrays.

### Example

Initially, we will convert the lists a and b to the sets and then apply intersection property “&” between the two sets to get the common elements.

a = [1, 2, 3, 4, 5]
b = [5, 2, 6, 3, 9]

result = (set(a) & set(b))

if result:
print("The common elements are:", result)
else:
print("No common elements present in two arrays")


### Output

The common elements are: {2, 3, 5}


For this example 2, 3, 5 are the common elements.

### Example

Let’s take another example to find the common elements.

a = [1, 2, 3, 4]
b = [5, 6, 7, 8]
result = (set(a) & set(b))
if result:
print("The common elements are:", result)
else:
print("No common elements present in two arrays")


### Output

No common elements present in two arrays


If no common element is found then the “&” property will return the empty set.

## Using NumPy

The numpy.intersect1d() method is used to find the common elements between two arrays. The intersect1d() method takes two arrays as parameters and it will return an array with all common elements.

### Syntax

numpy.intersect1d(ar1, ar2)


It finds the intersection of two arrays and returns the sorted, unique values that are present in both of the input arrays.

### Example

In this example, we will create the two numpy arrays using the numpy.array() method and then applied the numpy.intersect1d() method

import numpy as np

arr1 = np.array([1, 3, 4, 3])
arr2 = np.array([3, 1, 2, 1])

result = np.intersect1d(arr1, arr2)

print("The common elements are:", result)


### Output

The common elements are: [1 3]


From the above output the common elements are 1,3.

Updated on: 15-May-2023

2K+ Views 