Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,872 questions

51,796 answers

573 users

How to find all common elements in given three sorted lists with Python

2 Answers

0 votes
def PrintCommonElementsInThreelstays(lst1,  lst2,  lst3) :
    size1 = len(lst1)
    size2 = len(lst2)
    size3 = len(lst3)
        
    i = 0
    j = 0
    k = 0
    while (i < size1 and j < size2 and k < size3) :
        if (lst1[i] == lst2[j] and lst3[k] == lst1[i]) :
            print(str(lst1[i]) + " ", end ="")
            i += 1
            j += 1
            k += 1
        elif(lst1[i] < lst2[j]) :
            i += 1
        elif(lst2[j] < lst3[k]) :
            j += 1
        else :
            k += 1

lst1 = [2, 5, 6, 7, 9, 12, 20, 25, 30, 31]
lst2 = [4, 7, 10, 11, 20, 21, 30, 31, 37]
lst3 = [1, 2, 5, 7, 9, 18, 19, 20, 31, 32, 38, 39, 40, 50]

PrintCommonElementsInThreelstays(lst1, lst2, lst3)
    
 
 
 
'''
run:
 
7 20 31 

'''

 



answered Sep 21, 2022 by avibootz
0 votes
def PrintCommonElementsInThreelstays(lst1,  lst2,  lst3) :
    lst1 = set(lst1)
    lst2 = set(lst2)
    lst3 = set(lst3)
    
    common_elements = lst1 & lst2 & lst3
    
    print(common_elements)

lst1 = [2, 5, 6, 7, 9, 12, 20, 25, 30, 31]
lst2 = [4, 7, 10, 11, 20, 21, 30, 31, 37]
lst3 = [1, 2, 5, 7, 9, 18, 19, 20, 31, 32, 38, 39, 40, 50]

PrintCommonElementsInThreelstays(lst1, lst2, lst3)
    
 
 
 
'''
run:
 
{20, 7, 31}

'''

 



answered Sep 21, 2022 by avibootz
...