intersection_update() in Python to find common elements in n arrays
We are given list of n number of arrays, find all common elements in given arrays ?
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).
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 email@example.com. 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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.