
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python program to check if two lists have at least one common element
In this problem we use two user input list. Our task is to check that there is any common element or not. We use very simple traversing technique, traverse both the list and check the every element of first list and second list.
Example
Input : A = [10, 20, 30, 50] B = [90, 80, 30, 10, 3] Output : FOUND Input : A = [10, 20, 30, 50] B = [100,200,300,500] Output : NOT FOUND
Algorithm
commonelement(A,B) /* A and B are two user input list */ Step 1: First use one third variable c which is display the result. Step 2: Traverse both the list and compare every element of the first list with every element of the second list. Step 3: If common element is found then c display FOUND otherwise display NOT FOUND.
Example Code
# Python program to check # if two lists have at-least # one element common # using traversal of list def commonelement(A, B): c = "NOT FOUND" # traverse in the 1st list for i in A: # traverse in the 2nd list for j in B: # if one common if i == j: c="FOUND" return c return c # Driver code A=list() B=list() n1=int(input("Enter the size of the first List ::")) print("Enter the Element of first List ::") for i in range(int(n1)): k=int(input("")) A.append(k) n2=int(input("Enter the size of the second List ::")) print("Enter the Element of second List ::") for i in range(int(n2)): k=int(input("")) B.append(k) print("Display Result ::",commonelement(A, B))
Output
Enter the size of the first List ::4 Enter the Element of first List :: 2 1 4 9 Enter the size of the second List ::5 Enter the Element of second List :: 9 90 4 89 67 Display Result :: FOUND Enter the size of the first List ::4 Enter the Element of first List :: 67 89 45 23 Enter the size of the second List ::4 Enter the Element of second List :: 1 2 3 4 Display Result :: NOT FOUND
- Related Questions & Answers
- C# program to check if two lists have at-least one element common
- Python - Check if two lists have any element in common
- How to aggregate two lists if at least one element matches in MongoDB?
- Check if both halves of the string have at least one different character in Python
- Program to check every sublist in a list containing at least one unique element in Python
- Checking if decimals share at least two common 1 bits in JavaScript
- How to check if a string has at least one letter and one number in Python?
- Count divisors of n that have at-least one digit common with n in Java
- Check if two lists are identical in Python
- Program to check whether every one has at least a friend or not in Python
- Python program to print all the common elements of two lists.
- C++ program to count how many minutes we have to wait to meet at least one swimmer
- Check if element exists in list of lists in Python
- Program to find k where k elements have value at least k in Python
- Python program to check whether two lists are circularly identical
Advertisements