Python Program to Find Largest Element in an Array (original) (raw)
Last Updated : 06 Mar, 2024
To find the largest element in an array, iterate over each element and compare it with the current largest element. If an element is greater, update the largest element. At the end of the iteration, the largest element will be found.
Given an array, find the largest element in it.
**Input : arr[] = {10, 20, 4}
**Output : 20
**Input : arr[] = {20, 10, 20, 4, 100}
**Output : 100
**Find largest element in an array Using Native Approach
Python3
def
largest(arr, n):
`` max
=
arr[
0
]
`` for
i
in
range
(
1
, n):
`` if
arr[i] >
max
:
`` max
=
arr[i]
`` return
max
arr
=
[
10
,
324
,
45
,
90
,
9808
]
n
=
len
(arr)
Ans
=
largest(arr, n)
print
(
"Largest in given array "
, Ans)
Output
Largest in given array 9808
**Time Complexity: O(N)
**Auxiliary Space: O(1)
**Find largest element in an array Using built-in function max()
Here we will use the inbuilt method **max() to find the maximum of the array. Below is the implementation of the approach.
Python3
def
largest(arr, n):
`` ans
=
max
(arr)
`` return
ans;
if
__name__
=
=
'__main__'
:
`` arr
=
[
10
,
324
,
45
,
90
,
9808
]
`` n
=
len
(arr)
`` print
(
"Largest in given array "
, largest(arr, n))
Output
Largest in given array 9808
**Time Complexity: O(n), where n is length of list.
**Auxiliary Space: O(1)
**Find largest element in an array Using sort() function
Here we use the **sort() function to sort the array. The largest element will be the last element of the sorted array. Below is the implementation of the approach.
Python3
def
largest(arr, n):
`` arr.sort()
`` return
arr[n
-
1
]
arr
=
[
10
,
324
,
45
,
90
,
9808
]
n
=
len
(arr)
Ans
=
largest(arr, n)
print
(
"Largest in given array "
, Ans)
Output
Largest in given array 9808
**Find largest element in an array Using reduce() function
Here we use the reduce() function to iterate over the array. We will find the largest element the max function which will compare each element of array while iterating.
Below is the implementation of the above approach:
Python3
from
functools
import
reduce
def
largest(arr):
`` ans
=
reduce
(
max
, arr)
`` return
ans
arr
=
[
10
,
324
,
45
,
90
,
9808
]
n
=
len
(arr)
Ans
=
largest(arr)
print
(
"Largest in given array "
, Ans)
Output
Largest in given array 9808
**Time complexity: O(n) where n is size of given array
**Auxiliary space: O(1)
Please refer complete article on Program to find largest element in an array for more details!
**Find largest element in an array Using operator.gt()
Create a array of integers and assign it to the variable arr. Print the original array using the print() function.Create an empty array and assign it to the variable arr1.
Sort the original array in descending order using the operator.gt() function and assign the sorted array to the variable arr1. Print the biggest number in the array by accessing the first element of the sorted array using arr1[0]. Print a message indicating that the biggest number in the array has been found.
Python3
import
operator
arr
=
[
2
,
1
,
7
,
3
,
0
]
max
=
0
print
(
'The given array is:'
, arr)
for
i
in
arr:
`` if
operator.gt(i,
max
):
`` max
=
i
print
(
'The biggest number in the given array is:'
,
max
)
Output
The given array is: [2, 1, 7, 3, 0] The biggest number in the given array is: 7
**Time Complexity : O(n)
**Auxiliary Space : O(1)
Find Largest Element with Python Lambda
In this program, we have an array called array with some elements. We use the max function to find the largest element in the array. The key parameter is set to a lambda function lambda x: x, which simply returns the element itself. This lambda function is used to determine the comparison key for the max function.
Python3
array
=
[
10
,
5
,
20
,
8
,
15
]
largest_element
=
max
(array, key
=
lambda
x: x)
print
(
"Largest element in the array:"
, largest_element)
**Output:
Largest element in the array: 20