Python program to find common array elements


While considering the multi-dimensional arrays as an example, there is a method that is capable of finding the common elements present within a multi-dimensional array - intersection_update().

This method is used in order to find the common or intersecting elements present within the same array which is multi-dimensional in nature. Let us consider an input output scenario and then proceed with a program.

Input Output Scenarios

Consider a 2D array which is multi-dimensional in nature.

arr = [[1, 2, 3, 4], [3, 4, 5, 6], [7, 8, 3, 4], [4, 9, 8, 3], [4, 3, 10, 12]]
  • The above array consists of 5 sub arrays.

  • We can clearly observe that the elements “ 3 ” and “ 4 ” are present all the sub arrays of “ arr ”.

  • So, the elements “ 3 ” and “ 4 ” are considered to be the common elements of the 2D array arr.

Example

In this example, we are going to find the common elements present within a multi dimensional array using the method intersection_update().

  • Consider a two dimensional array from which the common elements can be found.

  • Declare a parameterized method which can find the common elements taking the 2D array as a parameter.

  • Within the method, initiate the set array with 0 and assign the value to a variable.

  • Traverse the elements of the array using a loop.

  • With the help of the method intersection_update(), find the common elements one after another while traversing.

def common_elements(arr):
   result = set(arr[0])
   for x in arr[1:]:
      result.intersection_update(x) 
   return list(result)
# main section
if __name__ == "__main__":

   arr = [[1, 2, 3, 4], [3, 4, 5, 6], [7, 8, 3, 4], [4, 9, 8, 3], [4, 3, 10, 12]]
   res = common_elements(arr)
   if len(res) > 0:
      print ("The common elements present within the array are: ",res)

   else:
      print ("There are no common elements present within the array !!")

Output

The output of the above program is as follows −

The common elements present within the array are:  [3, 4]

Finding Common Elements in two Different Arrays

The NumPy intersect1d() method can be used to find the common elements of two one-dimensional arrays. This is similar to the intersect_update() method, which deals with multi-dimensional arrays. To better understand this concept, let's look at an example.

Input Output Scenarios

Consider two arrays which are one dimensional in nature.

arr1 = [1, 2, 3, 4]
arr2 = [3, 4, 5, 6]
  • We can clearly see that the elements “ 3 ” and “ 4 ” are present in both arrays arr1 and arr2.

  • So, we can conclude that the common elements of the arrays arr1 and arr2 are 3 and 4.

Example

In the following example, we are going to find the common elements present within multiple one dimensional arrays using the method intersect1d() of the numpy module.

import numpy as n
arr1 = [1, 2, 3, 4]
print("The first array is: ")
print(arr1)
arr2 = [3, 4, 5, 6]
print("The second array is: ")
print(arr2)
narr1 = n.array(arr1)
narr2 = n.array(arr2)
print("The common elements within the given arrays are: ")
print(n.intersect1d(narr1, narr2))

Output

The output of the above program is as follows −

The first array is:
[1, 2, 3, 4]
The second array is:
[3, 4, 5, 6]
The common elements within the given arrays are:
[3 4]

In this way, depending on the type of arrays, the procedure can be followed. If the array considered is a multi-dimensional array, then the procedure followed in the first program will be followed.

If the arrays considered are one-dimensional arrays, then the procedure followed in the second program will be followed. This is how the common elements of one or more arrays are found.

Updated on: 05-May-2023

137 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements