# Find the Smallest and largest element in an array using Python

## Smallest and Largest Element in an array using Python

Here, in this page we will discuss the program to find the smallest and largest element in an array using python programming language. We will discuss different algorithms to find the smallest and largest element of the given input array.

## Method Discussed are given below :

• Method 1 : Using Iteration.
• Method 2 : Using Sort() function.
• Method 3 : Using max() and min() function

## Method 1 :

• Take a variable say mini to store the minimum element of the array and maxi to store the maximum element.
• Set mini = arr[0] and maxi = arr[0]
• Run a loop over the array
• Check if(arr[i]<mini) then set mini = arr[i]
• Also check if(arr[i]>maxi) then set maxi = arr[i]
• After complete iteration print mini and maxi.
•

### Method 1 : Code in Python

Run
```arr = [10, 89, 9, 56, 4, 80, 8]
mini = arr[0]
maxi = arr[0]

for i in range(len(arr)):
if arr[i] < mini: mini = arr[i]

if arr[i] > maxi: maxi = arr[i]

print (mini)
print (maxi)```

`489`

## Method 2 :

• Sort the array using inbuilt sort()function
• Minimum element is at index 0 and maximum is at index -1
• So, print(arr[0])
• And, print(arr[-1])

### Method 2 : Code in Python

Run
```arr = [10, 89, 9, 56, 4, 80, 8]
arr.sort()

print (arr[0])
print (arr[-1])```

`489`

## Method 3 :

• Using inbuilt function min(arr) , it return minimum element of the array.
• Similarly max(arr) return the maximum element of the array.

### Method 3 : Code in Python

Run
```arr = [10, 89, 9, 56, 4, 80, 8]

print (min(arr))
print (max(arr))```

`489`

### 3 comments on “Find the Smallest and largest element in an array using Python”

• Prashant

size=int(input(“enter the lentgh of array”))
arr=[]
for i in range(size):
arr.append(int(input(“Enter the Values–>”)))
arr.sort()
print(“the smallest value”,min(arr),”the gretaes value”,max(arr))

• Anmol

The written code will mislead students a lot please do change with something like this:
n=int(input(“Enter number of elements:”))
a=[int(i) for i in input(“Enter space seprated element:”).split()]
s=set(a)
ls=list(s)
ls.sort()
print(“Largest element=”,ls[-1])
print(“Smallest element=”,ls[0])

• Anmol

The written will mislead students a lot please do change with something like this:
n=int(input(“Enter number of elements:”))
a=[int(i) for i in input(“Enter space seprated element:”).split()]
s=set(a)
ls=list(s)
ls.sort()
print(“Largest element=”,ls[-1])
print(“Smallest element=”,ls[0])