Python Program to Find Most Common Elements Set


In Python, a set is an unordered collection of unique elements, represented by {}. It allows efficient membership testing and eliminates duplicate values, making it useful for tasks such as removing duplicates or checking for common elements between sets.

In this article, we will learn a python program to find the most common elements set.

Methods Used

The following are the various methods to accomplish this task:

  • Using for loop and set.intersection() function

  • Using list comprehension, max() and intersection() functions

Demonstration

Assume we have taken an input list containing sets. We will now get the most common elements using the above methods.

Input

inputList = [{9, 1, 3, 2}, {4, 5, 8, 1},
             {1, 4, 7, 8}, {9, 5, 5, 4}]
argumentSet = {5, 4, 9, 1}

Output

Input list: [{9, 2, 3, 1}, {8, 1, 4, 5}, {8, 1, 4, 7}, {9, 4, 5}]
The most common elements in the input set are: {8, 1, 4, 5}

Here the set {8,1,4,5} contains the most common elements i.e (4,5,1) from the given argument set {5,4,9,1}.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task –.

  • Create a variable to store the input list of sets.

  • Print the input list.

  • Create another variable to store the input argument set.

  • Create an empty set using the set() function(creates a set object. A set list will appear in random order because the items are not ordered. It removes all the duplicates) for storing the resultant set.

  • Initialize maximum length as 0.

  • Use the for loop to traverse through each element of the input list of sets.

  • Use the if conditional statement to check whether the number of common elements is greater than the maximum length variable.

  • If it is true then assign maximum length with these number of common elements.

  • Assign this current set as the result.

  • Print the resultant set containing the most common elements in an input list of sets.

Example 1: Using for loop and set.intersection() function

In this example, we are going to use for loop and set.intersection() function to find the most common element in a given set.

set.intersection() function: A set containing the similarity between two or more sets is what the intersection() method returns.

It means Only items that are present in both sets, or in all sets if more than two sets are being compared, are included in the returned set.

len() function: The number of items in an object is returned by the len() method.The len() function returns the number of characters in a string when the object is a string.

Example

The following program returns a set containing the most common elements in an input set using for loop and set.intersection() function

# input list of sets
inputList = [{9, 1, 3, 2}, {4, 5, 8, 1},
             {1, 4, 7, 8}, {9, 5, 5, 4}]
# printing the input list
print("Input list:", inputList)
# input argument set
argumentSet = {5, 4, 9, 1}
# creating an empty set for storing resultant set
resultantSet = set()
# initiliazing maximum length as 0
maxLength = 0
# traversing through the input list
for i in inputList:
    # Checking the common elements is greater than the maximum length variable
    if len(i.intersection(argumentSet)) > maxLength:
        # If the above condition is true then add these common elements to the max length
        maxLength = len(i.intersection(argumentSet))
        # assigning a current element to the resultant set
        resultantSet = i
# printing resultant Set
print("The Most common elements in the input set are:", resultantSet)

Output

On executing, the above program will generate the following output

Input list: [{9, 2, 3, 1}, {8, 1, 4, 5}, {8, 1, 4, 7}, {9, 4, 5}]
The most common elements in the input set are: {8, 1, 4, 5}

Example 2: Using list comprehension, max() and intersection() functions

In this method we are going to learn how to use a simple combination of list comprehension, max() and intersection() function to find the most common element set.

List Comprehension

When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.

max() function

The max() method returns the highest-valued item/greatest number in an iterable The following program returns a set containing the most common elements in an input set using list comprehension, max(), and intersection() functions

# input list of sets
inputList = [{9, 1, 3, 2}, {4, 5, 8, 1},
             {1, 4, 7, 8}, {9, 5, 5, 4}]
# printing the input list
print("Input list:", inputList)
# input argument set
argumentSet = {5, 4, 9, 1}
# Get the most number of common elements from the given list of sets
maxLength = max(len(i.intersection(argumentSet)) for i in inputList)
# Getting the set with the above number of common elements
resultantSet = [i for i in inputList if len(i.intersection(argumentSet)) == maxLength][0]
# printing resultant Set
print("Most common elements in input set are:", resultantSet)

Output

On executing, the above program will generate the following output –

Input list: [{9, 2, 3, 1}, {8, 1, 4, 5}, {8, 1, 4, 7}, {9, 4, 5}]
The most common elements in the input set are: {8, 1, 4, 5}

Conclusion

In this article, we have learned 2 different methods to Find the Most common elements set from the given list of sets. Additionally, we learned how to use the intersection function to extract the common elements from the given two sets.

Updated on: 17-Aug-2023

241 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements