Python
- key differences between List and Arrays
- Python collections ChainMap
- Array : Find median in an integer array
- Array : Find middle element in an integer array
- Array : Find out the duplicate in an array
- Array : Find print all subsets in an integer array
- Program : Array : Finding missing number between from 1 to n
- Array : Gap and Island problem
- Python collections
- Python Program stock max profit
- Reverse words in Python
- Python array duplicate program
- Coin change problem in python
- Python Write fibonacci series program
- Array : find all the pairs whose sum is equal to a given number
- Find smallest and largest number in array
- Iterate collections
- List comprehensions in Python
- key differences between List and Arrays
- Program: Calculate Pi in Python
- String Formatting in Python
- Python counters
- python tuples
- Python deque
- Python dictionary
- Python Lists
- python namedtuple
How to find small and largest number from array?
In this post, we explore the best way to find the smallest and largest number in an array using Python. We will provide step-by-step implementation of the algorithm, which involves traversing the array once to identify the smallest and largest elements.
By utilizing this approach, we can determine the minimum and maximum values in the array with a linear time complexity, making it suitable for large datasets. Understanding how to find the smallest and largest numbers in an array is fundamental for various data manipulation and statistical operations in programming and data analysis
def find_largest_smallest(numbers):
if not numbers:
return None # Return None for an empty array
smallest = largest = numbers[0] # Initialize smallest and largest with the first element
for num in numbers:
if num < smallest:
smallest = num
elif num > largest:
largest = num
return smallest, largest
integer_array = [5, 2, 9, 1, 7, 4]
smallest_number, largest_number = find_largest_smallest(integer_array)
print("Smallest number:", smallest_number)
print("Largest number:", largest_number)
#Output
Smallest number: 1
Largest number: 9