Python Program To Find Largest Element In An Array

Python

Nicesnippets

382

23-06-2021


Python Program To Find Largest Element In An Array

Hello Friends,

Now let's see example of how to get largest element of array in python. I would like to share with you find the largest element in that given array using python. We will show python get highest element in array.

In this article, I will give simple and easy way to get max element of array using python.

Here i will give you two example for how to get max element of array in python. So let's see the below example:

Example 1


#Initialize array

arr = [88, 15, 70, 30, 85]

#Initialize max with first element of array.

max = arr[0]

#Loop through the array

for i in range(0, len(arr)):

#Compare elements of array with max

if(arr[i] > max):

max = arr[i]

print("Largest element is: " + str(max))

Output

Largest element is: 88

Example 2

# Python3 program to find maximum

# in arr[] of size n

# python function to find maximum

# in arr[] of size n

def findLargest(arr,n):

# Initialize maximum element

max = arr[0]

# Traverse array elements from second

# and compare every element with

# current max

for i in range(1, n):

if arr[i] > max:

max = arr[i]

return max

# Array

arr = [20, 55, 900, 566, 8]

n = len(arr)

largeElement = findLargest(arr,n)

print ("Largest element is",largeElement)

Output

Largest element is: 900

It will help you....


Recommended Posts