Python Program to Find the Common Elements in the N Sets

Common Elements in N Sets

Write a program to find the common elements in the N sets.


The first line of input will contain a positive integer (N).
The next N lines will contain space-separated integers, denoting the elements of each set.


The output should be a single line containing the list of common elements in N sets sorted in ascending order.


For example, if the given N is 3, read inputs in the next three lines. If the given three strings are as the following.
2 4 6 8 10
48 10 12 16
5 10 15 20

As 10 is common in all three sets, the output should be


Sample Input 1
24 6 8 10
4 8 10 12 16
5 10 15 20

Sample Output 1


def convert_string_to_int(list_a):
    new_list = []
    for item in list_a:
        num = int(item)
    return new_list

def get_intersection_of_n_sets(num_set_list):
    result = num_set_list[0]
    for num_set in num_set_list:
        result = result.intersection(num_set)
    return result

n = int(input())
num_set_list = []
for i in range(n):
    values_list = input().split()
    values_list = convert_string_to_int(values_list)
    values_set = set(values_list)

result_set = get_intersection_of_n_sets(num_set_list)
result_list = list(result_set)

24 6 8 10 12
24 8 12 16 10
2 5 10 12 15 20 4
12 10 4 8

[4, 10, 12]

Related Posts

Python Program to Convert Integer into a Single Numeral

Single Digit Number Ram is given a positive integer N .He wishes to convert this integer into a single numeral. He does so by repeatedly adding the…

Python Program to Print Armstrong numbers between two intervals

Write a program to print all the Armstrong numbers in the given range A to B (including A and B). An N – digit number is an…

Python Program to Check if Given Input is a Letter, Digit or Special Character

Letter, Digit or Special Character You are given a character as input. Check if the given input is a Lowercase Letter or Uppercase Letter or Digit or…

Interleave Strings | Python Program to merge two strings by adding characters in alternating order

Interleave Strings Given two strings, write a program to merge the given two strings by adding characters in alternating order, starting with the first string. If a…

Python Program to Check if given colors are present in the color palate

Shekhar is an artist. He likes to draw and paint. He is now drawing a portrait of a city road which is in the black color and…

Python Program to Check the eligibility criteria for donor  

Python Comparison Operators An XYZ hospital has organised a blood donation camp. The eligibility criteria for donor was as follows  1) Age>=18  2) Weight >=50 kg  3)…

Leave a Reply

Your email address will not be published. Required fields are marked *