Home » Python » Python programs

# Python program to find the largest element in an array

Here, we are going to learn **how to find the largest element in an array in Python?**

Submitted by Shivang Yadav, on April 04, 2021

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Array in Python is data structures that are used to store multiple data in a single variable.

**Example:** *studentArray[] = {"John", "Prem", "Kiran", "Joe"}*

In this program, we will take the array and its size as input from the user and print the largest value of the array.

**Example:**

Input: 7 4 5 9 1 12 8 3 Output: The largest element of the array is 12

To **find the largest element of the array**, we will initialize a *maxVal* variable with the first value of the array *arr[0]*. Then looping from 1 to *n*, we will compare *maxVal* with all values of the array. And if any value is greater than *maxVal*, we will replace *maxVal* with it. At last, we will print the *maxVal*.

**Algorithm:**

- Initialize
*maxVal = arr[0]*. - Loop through the array for
*i -> 1 to n*.*if (arr[i] > maxVal), then maxVal = arr[i]*.

- Print
*maxVal*.

**Program to find the largest element in an array**

# Python program to find the Largest element of the array # function to find the Largest element of array def FindLargestElement(arr,n): maxVal = arr[0] for i in range(1, n): if arr[i] > maxVal: maxVal = arr[i] return maxVal # code to take user input for the array... n = int(input("Enter the number of elements in the array: ")) arr = [] print("Enter elements of the array: ") for i in range(n): numbers = int(input()) arr.append(numbers) # Calling the method to find the largest value maxVal = FindLargestElement(arr,n) print ("Largest in given array is",maxVal)

**Output:**

Enter the number of elements in the array: 5 Enter elements of the array: 10 12 45 10 5 Largest in given array is 45

**Explanation:**

In the above code, we have taken the array as input from the user and stored it in the variable *arr*. Then we have called a method named *findLargestElement()* which will compute the maximum value of the array and return it. This return value is stored in a *maxVal* variable and then printed.

**findLargestElement() function:** the function accepts the array and its size as input and returns the maximum value of the array.

The function initializes a variable *maxVal* with the first value of the array. Then loops through the array and if the value of *arr[i]* is greater than *maxVal*, puts the value of *arr[i]* to *maxVal*. After the loop ends the function returns the *maxVal*.

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions