Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Find common elements in three sorted arrays by dictionary intersection in Python
When working with Python data manipulation, you may need to find elements that are common among multiple arrays. This can be efficiently achieved by converting arrays into dictionaries using the Counter class from the collections module.
The approach involves using Counter to count occurrences of each element, then finding the intersection using the & operator. This method preserves the minimum count of common elements across all arrays.
Using Counter and Dictionary Intersection
Here's how to find common elements using dictionary intersection ?
from collections import Counter
arrayA = ['Sun', 12, 14, 11, 34]
arrayB = [6, 12, 'Sun', 11]
arrayC = [19, 6, 20, 'Sun', 12, 67, 11]
# Convert arrays to Counter objects
counterA = Counter(arrayA)
counterB = Counter(arrayB)
counterC = Counter(arrayC)
# Find intersection of all counters
commonDict = dict(counterA.items() & counterB.items() & counterC.items())
# Extract common elements with their minimum counts
result = []
for (key, val) in commonDict.items():
for i in range(0, val):
result.append(key)
print("The common values among the arrays are:")
print(result)
The common values among the arrays are: ['Sun', 11, 12]
How It Works
The Counter class creates a dictionary-like object that counts occurrences of each element. When you use the & operator between Counter objects, it returns the intersection with the minimum count for each common key.
Step-by-Step Process
from collections import Counter
arrayA = ['Sun', 12, 11, 11]
arrayB = [12, 'Sun', 11]
arrayC = ['Sun', 12, 11, 11, 11]
counterA = Counter(arrayA)
counterB = Counter(arrayB)
counterC = Counter(arrayC)
print("Counter A:", counterA)
print("Counter B:", counterB)
print("Counter C:", counterC)
intersection = counterA & counterB & counterC
print("Intersection:", intersection)
Counter A: Counter({'Sun': 1, 12: 1, 11: 2})
Counter B: Counter({12: 1, 'Sun': 1, 11: 1})
Counter C: Counter({'Sun': 1, 12: 1, 11: 3})
Intersection: Counter({'Sun': 1, 12: 1, 11: 1})
Alternative Approach Using Set Intersection
For a simpler solution when you only need unique common elements ?
arrayA = ['Sun', 12, 14, 11, 34]
arrayB = [6, 12, 'Sun', 11]
arrayC = [19, 6, 20, 'Sun', 12, 67, 11]
# Convert to sets and find intersection
common_elements = set(arrayA) & set(arrayB) & set(arrayC)
result = list(common_elements)
print("Common elements:", result)
Common elements: [11, 12, 'Sun']
Comparison
| Method | Preserves Duplicates? | Best For |
|---|---|---|
| Counter Intersection | Yes (minimum count) | When duplicate counts matter |
| Set Intersection | No | Unique elements only |
Conclusion
Use Counter intersection when you need to preserve duplicate elements with their minimum occurrence counts. For unique common elements only, set intersection provides a simpler solution.
