- Trending Categories
- Data Structure
- Operating System
- C Programming
- 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 - Check if a number and its double exists in an array
When it is required to check if a number and its double exists in an array, it is iterated over, and multiple with 2 and checked.
Below is a demonstration of the same
def check_double_exists(my_list): for i in range(len(my_list)): for j in (my_list[:i]+my_list[i+1:]): if 2*my_list[i] == j: print("The double exists") my_list = [67, 34, 89, 67, 90, 17, 23] print("The list is :") print(my_list) check_double_exists(my_list)
The list is : [67, 34, 89, 67, 90, 17, 23] The double exists
A method named ‘check_double_exists’ is defined that takes a list as a parameter.
It iterates through the list, and multiple every element with 2 and checks to see if there exists a number that matches this doubled value.
If such a value is found, relevant message is displayed.
Outside the method, a list is defined, and is displayed on the console.
The method is called by passing the list.
The output is displayed on the console.
- Python - Check if a number and its triple exists in an array
- Check if a PHP cookie exists and if not set its value
- Check if subarray with given product exists in an array in Python
- How to check if an item exists in a C# array?
- Check if table exists in MySQL and display the warning if it exists?
- How do I check if a Python variable exists?
- Check if a file exists in Java
- Check if a File exists in C#
- Check if a user exists in MySQL and drop it?
- Check if an array is sorted and rotated in Python
- How to check if a key exists in a Python dictionary?
- Check if MongoDB database exists?
- How to check if an alert exists using WebDriver?
- Check if a word exists in a grid or not in Python