Given three arrays sorted in non-decreasing order, print all common elements in these arrays.

Examples:

Input: ar1 = [1, 5, 10, 20, 40, 80] ar2 = [6, 7, 20, 80, 100] ar3 = [3, 4, 15, 20, 30, 70, 80, 120] Output: [80, 20] Input: ar1 = [1, 5, 5] ar2 = [3, 4, 5, 5, 10] ar3 = [5, 5, 10, 20] Output: [5, 5]

We have existing solution for this problem please refer Find common elements in three sorted arrays link. We can solve this problem quickly in python using **intersection** of dictionaries. Approach is simple,

- First convert all three lists into dictionaries having elements as keys and their frequencies as value, using Counter() method.
- Now perform intersection operation for three dictionaries, this will result us dictionary having common elements among three array list with their frequencies.

`# Function to find common elements in three ` `# sorted arrays ` `from` `collections ` `import` `Counter ` ` ` `def` `commonElement(ar1,ar2,ar3): ` ` ` `# first convert lists into dictionary ` ` ` `ar1 ` `=` `Counter(ar1) ` ` ` `ar2 ` `=` `Counter(ar2) ` ` ` `ar3 ` `=` `Counter(ar3) ` ` ` ` ` `# perform intersection operation ` ` ` `resultDict ` `=` `dict` `(ar1.items() & ar2.items() & ar3.items()) ` ` ` `common ` `=` `[] ` ` ` ` ` `# iterate through resultant dictionary ` ` ` `# and collect common elements ` ` ` `for` `(key,val) ` `in` `resultDict.items(): ` ` ` `for` `i ` `in` `range` `(` `0` `,val): ` ` ` `common.append(key) ` ` ` ` ` `print` `(common) ` ` ` `# Driver program ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `ar1 ` `=` `[` `1` `, ` `5` `, ` `10` `, ` `20` `, ` `40` `, ` `80` `] ` ` ` `ar2 ` `=` `[` `6` `, ` `7` `, ` `20` `, ` `80` `, ` `100` `] ` ` ` `ar3 ` `=` `[` `3` `, ` `4` `, ` `15` `, ` `20` `, ` `30` `, ` `70` `, ` `80` `, ` `120` `] ` ` ` `commonElement(ar1,ar2,ar3) ` |

*chevron_right*

*filter_none*

Output:

[80, 20]

Attention geek! Strengthen your foundations with the **Python Programming Foundation** Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the **Python DS** Course.

## Recommended Posts:

- Python | Dictionary initialization with common dictionary
- Python program to find common elements in three lists using sets
- Python counter and dictionary intersection example (Make a string using deletion and rearrangement)
- Intersection of two arrays in Python ( Lambda expression and filter function )
- Python - Common list elements and dictionary values
- Python program to check if the list contains three consecutive common numbers in Python
- intersection_update() in Python to find common elements in n arrays
- Python | Combine two dictionary adding values for common keys
- Python | Initialize dictionary with common value
- Python - Common items Dictionary Value List
- Python - Common keys in list and dictionary
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python | Convert string dictionary to dictionary
- Python | Pretty Print a dictionary with dictionary value
- Regular Dictionary vs Ordered Dictionary in Python
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Convert Dictionary Value list to Dictionary List

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.