Thursday, December 26, 2024
Google search engine
HomeLanguagesPython | Count of common elements in the lists

Python | Count of common elements in the lists

Sometimes, while working with Python list we can have a problem in which we have to compare two lists for index similarity and hence can have a task of counting equal index pairs. Let’s discuss certain ways in which this task can be performed.

Method #1: Using sum() + zip() This task can be performed by passing the zip(), which performs task of mapping both list with each other, to the sum() which computes the sum according to equal indices. 

Python3




# Python3 code to demonstrate working of
# Identical element summation in lists
# using sum() + zip()
 
# initialize lists
test_list1 = [5, 6, 10, 4, 7, 1, 19]
test_list2 = [6, 6, 10, 3, 7, 10, 19]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Identical element summation in lists
# using sum() + zip()
res = sum(x == y for x, y in zip(test_list1, test_list2))
 
# printing result
print("Summation of Identical elements : " + str(res))


Output : 

The original list 1 is : [5, 6, 10, 4, 7, 1, 19]
The original list 2 is : [6, 6, 10, 3, 7, 10, 19]
Summation of Identical elements : 4

Time complexity: O(n), where n is the length of the input lists.
Auxiliary space: O(1).

Method #2 : Using sum() + map() + eq The task performed in above method using zip() can be executed here using the map function which performs similar task. The equality check can be performed by inbuilt eq operator. 

Python3




# Python3 code to demonstrate working of
# Identical element summation in lists
# using sum() + map() + eq
from operator import eq
 
# initialize lists
test_list1 = [5, 6, 10, 4, 7, 1, 19]
test_list2 = [6, 6, 10, 3, 7, 10, 19]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Identical element summation in lists
# using sum() + map() + eq
res = sum(map(eq, test_list1, test_list2))
 
# printing result
print("Summation of Identical elements : " + str(res))


Output : 

The original list 1 is : [5, 6, 10, 4, 7, 1, 19]
The original list 2 is : [6, 6, 10, 3, 7, 10, 19]
Summation of Identical elements : 4

Time complexity: O(n)
Auxiliary space: O(1)

Method #3: Using set() + len() The task performed can also be perform using the set and len function. We can get a common element using set intersection and count the total common element using len function. 

Python3




# Python3 code to demonstrate working of
# Identical element summation in lists
# using set() + let()
 
# initialize lists
test_list1 = [5, 6, 10, 4, 7, 1, 19]
test_list2 = [6, 6, 10, 3, 7, 10, 19]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Identical element summation in lists
# using set() + len()
res = len(set(test_list1) & set(test_list2));
 
# printing result
print("Summation of Identical elements : " + str(res))


Output

The original list 1 is : [5, 6, 10, 4, 7, 1, 19]
The original list 2 is : [6, 6, 10, 3, 7, 10, 19]
Summation of Identical elements : 4

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(1) additional space is not needed 

 Method#4: Using set intersection

Algorithm:

  1. Initialize two lists, test_list1 and test_list2, with the given values.
  2. Use the set intersection operator to get a set of common elements from both lists.
  3. Get the length of the set obtained from the above step.
  4. Assign the obtained value to res variable.
  5. Print the result.

Python3




# initialize lists
test_list1 = [5, 6, 10, 4, 7, 1, 19]
test_list2 = [6, 6, 10, 3, 7, 10, 19]
 
# using set intersection to get number of identical elements
res = len(set(test_list1) & set(test_list2))
 
# printing result
print("Summation of Identical elements : " + str(res))


Output

Summation of Identical elements : 4

Time complexity: O(n), where n is the length of the input lists. This is because we are only iterating over the two input lists once.
Auxiliary space: O(m), where m is the length of the set of common elements between the two input lists. This is because we are creating a set to hold the common elements between the two input lists.

Method #5: Using dictionary

  • Initialize an empty dictionary freq_dict.
  • Iterate through test_list1 and add the element as a key to freq_dict with a value of 1 if it doesn’t already exist, otherwise increment the existing value by 1.
  • Iterate through test_list2, for each element, if it exists in freq_dict, decrement its value by 1 and add it to the result variable res. If its value becomes 0, delete the key from freq_dict.
  • Print the value of res.

Python3




# Python3 code to demonstrate working of
# Identical element summation in lists
# using dictionary
 
# initialize lists
test_list1 = [5, 6, 10, 4, 7, 1, 19]
test_list2 = [6, 6, 10, 3, 7, 10, 19]
 
# printing original lists
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# Identical element summation in lists
# using dictionary
freq_dict = {}
res = 0
for i in test_list1:
    if i not in freq_dict:
        freq_dict[i] = 1
    else:
        freq_dict[i] += 1
 
for i in test_list2:
    if i in freq_dict and freq_dict[i] > 0:
        res += 1
        freq_dict[i] -= 1
 
# printing result
print("Number of Identical elements : " + str(res))


Output

The original list 1 is : [5, 6, 10, 4, 7, 1, 19]
The original list 2 is : [6, 6, 10, 3, 7, 10, 19]
Number of Identical elements : 4

Time complexity: O(n)
Auxiliary space: O(n)

RELATED ARTICLES

Most Popular

Recent Comments