Python Program to find the largest element in an array

PythonServer Side ProgrammingProgramming

In this article, we will learn about the solution to the problem statement given below.

Problem statement − We are given an array, we need to calculate the largest element of the array.

Here we use the bruteforce approach in which we compute the largest element by traversing the whole loop and get the element.

We can observe the implementation below.


 Live Demo

# largest function
def largest(arr,n):
   #maximum element
   max = arr[0]
   # traverse the whole loop
   for i in range(1, n):
      if arr[i] > max:
         max = arr[i]
   return max
# Driver Code
arr = [23,1,32,67,2,34,12]
n = len(arr)
Ans = largest(arr,n)
print ("Largest element given in array is",Ans)


Largest in given array is 67

All the variables are declared in global form as shown above


In this article, we have learned how to get the largest element from the array.

Updated on 23-Dec-2019 08:07:18