intersection_update() in Python to find common elements in n arrays Last Updated : 21 Jul, 2022 Comments Improve Suggest changes Like Article Like Report 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). Implementation: Python3 # 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 ("Common Element = ",output) else: print ('No Common Elements Found') OutputCommon Element = [2, 3] Comment More infoAdvertise with us Next Article intersection_update() in Python to find common elements in n arrays S Shashank Mishra (Gullu) Improve Article Tags : Python DSA Arrays Practice Tags : Arrayspython Similar Reads Find Common Elements in Two Arrays in Python 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 5Explanation: 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 4Explanati 8 min read Find common elements in three sorted arrays by dictionary intersection One way to efficiently find shared items in three sorted arrays is by using dictionary intersection. However, it's important to note that dictionaries are commonly used for unique keys, so if there are duplicate elements in your arrays, some adjustments may be needed to make this approach work. Give 4 min read Python | Merge List with common elements in a List of Lists Given a list of list, we have to merge all sub-list having common elements. These type of problems are very frequent in College examinations and while solving coding competitions. Below are some ways to achieve this. Input: [[11, 27, 13], [11, 27, 55], [22, 0, 43], [22, 0, 96], [13, 27, 11], [13, 27 3 min read Intersection of Two Arrays with Distinct Elements Given two arrays a[] and b[] with distinct elements of size n and m respectively, the task is to find intersection (or common elements) of the two arrays. We can return the answer in any order.Note: Intersection of two arrays can be defined as a set containing distinct common elements between the tw 9 min read Find common elements in three linked lists Given three linked lists, find all common elements among the three linked lists. Examples: Input : 10 15 20 25 12 10 12 13 15 10 12 15 24 25 26 Output : 10 12 15 Input : 1 2 3 4 5 1 2 3 4 6 9 8 1 2 4 5 10 Output : 1 2 4 Method 1 : (Simple) Use three-pointers to iterate the given three linked lists a 11 min read Like