Open In App

Python – Total equal pairs in List

Improve
Improve
Like Article
Like
Save
Share
Report

Given a list, the task is to write a python program to compute total equal digit pairs, i.e extract the number of all elements with can be dual paired with similar elements present in the list.

Input : test_list = [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3]
Output : 4

Explanation : 4, 2 and 5 have 3 occurrences, 7 has 2 occurrences, 1 pair each.

Input : test_list = [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3, 3]
Output : 5

Explanation : 4, 2 and 5 have 3 occurrences, 1 pair each and 3, 7 have 2 occurrences, 
total 5 pairs.

Method #1 : Using count() + set()

In this, we convert the container to set and use count() upon each element on the original list. Post that quotient with 2 is obtained and summed to get required pairs.

Python3




# Python3 code to demonstrate working of
# Total equal pairs in List
# Using loop + count()
 
# initializing lists
test_list = [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
all_ele = set(test_list)
res = 0
for ele in all_ele:
 
    # summing count from element list
    res += test_list.count(ele) // 2
 
# printing result
print("Total Pairs : " + str(res))


Output

The original list is : [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3]
Total Pairs : 4

Time Complexity: O(n), where n is the number of elements in the list.
Auxiliary Space: O(n), as we are using set data structure to store unique elements of the list.

Method #2 : Using Counter() + list comprehension + sum()

In this, getting the count of each element is done using Counter(), sum() is used to compute total pairs.

Python3




# Python3 code to demonstrate working of
# Total equal pairs in List
# Using Counter() + list comprehension + sum()
from collections import Counter
 
# initializing lists
test_list = [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using Counter for getting elements count
res = sum(ele // 2 for ele in Counter(test_list).values())
 
# printing result
print("Total Pairs : " + str(res))


Output

The original list is : [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3]
Total Pairs : 4

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), for the dictionary created by the Counter() function. The list comprehension and sum() functions do not require extra space proportional to the length of the input list.

Python3




# Python3 code to demonstrate working of
# Total equal pairs in List
 
# initializing lists
test_list = [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
all_ele = set(test_list)
res = 0
for ele in all_ele:
    if(test_list.count(ele)>=2):
        res+=1
 
# printing result
print("Total Pairs : " + str(res))


Output

The original list is : [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3, 3]
Total Pairs : 5

Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(k), where k is the number of unique elements in the input list (determined by creating a set of the input list).

Method 4 : using dictionary

step-by-step approach for the program:

  • Initialize a list of integers called test_list.
  • Print out the original list using the print() function and string concatenation.
  • Initialize an empty dictionary called freq.
  • Loop through each number num in test_list.
  • Check if num is already a key in freq.
  • If num is a key in freq, increment its value by 1.
  • If num is not a key in freq, add it to the dictionary with a value of 1.
  • Initialize a variable called res to 0.
  • Loop through each key-value pair in freq.
  • Check if the value is greater than or equal to 2.
  • If the value is greater than or equal to 2, add the integer division of the value by 2 to res.
  • Print out the total number of pairs found using the print() function and string concatenation.

Python3




# initializing lists
test_list = [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3, 3]
 
# printing original list
print("The original list is : " + str(test_list))
 
freq = {}
for num in test_list:
    if num in freq:
        freq[num] += 1
    else:
        freq[num] = 1
 
res = 0
for key, value in freq.items():
    if value >= 2:
        res += value // 2
 
# printing result
print("Total Pairs : " + str(res))


Output

The original list is : [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3, 3]
Total Pairs : 5

Time Complexity: O(n) where n is the length of the input list, as we need to traverse through the list only once to build the dictionary.

Auxiliary Space: O(n) to store the dictionary.

METHOD 5:Using quick sort

APPROACH:

The quick sort approach first uses the quick sort algorithm to sort the given list in ascending order. Then it iterates through the sorted list and counts the number of pairs of adjacent elements that are equal.

ALGORITHM:

1.Define a quick_sort function that takes a list lst as input.
2.If the length of the list is less than or equal to 1, return the list.
3.Choose the first element of the list as the pivot.
4.Create three new lists: equal to hold all elements equal to the pivot, left to hold all elements less than the pivot, and right to hold all elements greater than the pivot.
5.Recursively call the quick_sort function on left and right, and concatenate the results with equal to get the sorted list.
6.Define a count_pairs function that takes a list lst as input.
7.If the length of the list is less than or equal to 1, return 0.
8.Use the quick_sort function to sort the list.
9.Iterate through the sorted list and count the number of pairs of adjacent elements that are equal.
10.Return the count.

Python3




def quick_sort(lst):
    if len(lst) <= 1:
        return lst
     
    pivot = lst[0]
    equal = [x for x in lst if x == pivot]
    left = [x for x in lst if x < pivot]
    right = [x for x in lst if x > pivot]
     
    return quick_sort(left) + equal + quick_sort(right)
 
def count_pairs(lst):
    if len(lst) <= 1:
        return 0
     
    sorted_lst = quick_sort(lst)
     
    i = 0
    count = 0
    while i < len(sorted_lst)-1:
        if sorted_lst[i] == sorted_lst[i+1]:
            count += 1
            i += 2
        else:
            i += 1
     
    return count
 
lst = [2, 4, 5, 2, 5, 4, 2, 4, 5, 7, 7, 8, 3]
pairs = count_pairs(lst)
 
print("Total Pairs:", pairs)


Output

Total Pairs: 4

Time Complexity:
The time complexity of the quick sort algorithm is O(nlogn) in the average case and O(n^2) in the worst case. In this implementation, we use quick sort to sort the list, so the time complexity of quick_sort is O(nlogn) in the average case and O(n^2) in the worst case. The count_pairs function iterates through the sorted list once, so its time complexity is O(n). Therefore, the overall time complexity of this approach is O(nlogn) in the average case and O(n^2) in the worst case.

Auxiliary Space:
The space complexity of the quick sort algorithm is O(logn) in the average case and O(n) in the worst case. In this implementation, we create new lists equal, left, and right to hold elements less than, equal to, and greater than the pivot, so the space complexity of quick_sort is O(n) in the worst case. The count_pairs function uses constant extra space, so its space complexity is O(1). Therefore, the overall space complexity of this approach is O(n) in the worst case.



Last Updated : 05 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads