We are given list of n number of arrays, find all common elements in given arrays ?

Examples:

Input : arr = [[1,2,3,4], [8,7,3,2], [9,2,6,3], [5,1,2,3]] Output : Common Elements = [2,3]

We can solve this problem quickly in python using **intersection_update() method** of Set() data structure.

** How intersection_update() works ?**

Suppose we have two sets A and B, then A.intersection_update(B) operation updates set A with common elements in set A and B. For example, A=set([1,2,3]) and B=set([4,2,3]) now after taking **A.intersection_update(B)**, value of set A will be [2,3]. Syntax is **anySet.intersection_update(iterable)**.

`# Function to find common elements in n arrays ` `def` `commonElements(arr): ` ` ` ` ` `# initialize result with first array as a set ` ` ` `result ` `=` `set` `(arr[` `0` `]) ` ` ` ` ` `# now iterate through list of arrays starting from ` ` ` `# second array and take intersection_update() of ` ` ` `# each array with result. Every operation will ` ` ` `# update value of result with common values in ` ` ` `# result set and intersected set ` ` ` `for` `currSet ` `in` `arr[` `1` `:]: ` ` ` `result.intersection_update(currSet) ` ` ` ` ` `return` `list` `(result) ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `arr ` `=` `[[` `1` `,` `2` `,` `3` `,` `4` `], [` `8` `,` `7` `,` `3` `,` `2` `], [` `9` `,` `2` `,` `6` `,` `3` `], [` `5` `,` `1` `,` `2` `,` `3` `]] ` ` ` `output ` `=` `commonElements(arr) ` ` ` `if` `len` `(output) > ` `0` `: ` ` ` `print` `output ` ` ` `else` `: ` ` ` `print` `'No Common Elements Found'` |

*chevron_right*

*filter_none*

Output:

Common Elements = [2,3]

This article is contributed by **Shashank Mishra (Gullu)**. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Python | Find common elements in three sorted arrays by dictionary intersection
- Find common elements in three sorted arrays
- Python | Find common elements in list of lists
- Python program to find common elements in three lists using sets
- Count common elements in two arrays which are in Arithmetic Progression
- Count number of common elements between two arrays by using Bitset and Bitwise operation
- Python | Common Row elements Summation
- Python | Print all the common elements of two lists
- Python | Count of common elements in the lists
- Python - Test Common Elements Order
- Python | Print the common elements in all sublists
- Python - Common list elements and dictionary values
- Python | Find most common element in a 2D list
- Python | Find most common element in each column in a 2D list
- Find a pair of elements swapping which makes sum of two arrays same
- Find the ratio of number of elements in two Arrays from their individual and combined average
- Python | Merge List with common elements in a List of Lists
- Longest common subarray in the given two arrays
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Python List Comprehension to find pair with given sum from two arrays