Find Largest element in an array using Python
Largest Element in an array using python
Here, in this page we will discuss the program to find the largest element in an array using python we are given with an array elements and we need to print the largest among the given elements. We discuss different methods to find the largest element using python programing language.
In this page we will discuss the following ways to find the maximum element among the given elements of the array.
- Method 1 : Using Iteration
- Method 2 : Using max() function
- Method 3 : Using sort() function.
Method 1 :
- Declare a variable say max_element and initialize it with a[0].
- Run a loop for range a.len().
- Check if(a[i]>max_element) then set max_element to a[i]
- After complete iteration print(max_element)
Method 1 : Python code
Run
a = [10, 89, 9, 56, 4, 80, 8] max_element = a[0] for i in range(len(a)): if a[i] > max_element: max_element = a[i] print (max_element)
Output :
89
Method 2 :
- Sort the array using sort() function.
- Print the last element of the array, i.e at index -1 (a[-1])
Method 2 : Python code
Run
a = [10, 89, 9, 56, 4, 80, 8] a.sort() print (a[-1])
Output :
89
Method 3 :
Using max() inbuilt function.
- Declare an array.
- Print the max(arr)
Method 3 : Python Code
Run
arr = [10, 89, 9, 56, 4, 80, 8] print (max(arr))
Output :
89
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment