 GeeksforGeeks App
Open App Browser
Continue

# Python Program to find largest element in an array

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

Approach 1:

## Python3

 `# Python3 program to find maximum``# in arr[] of size n` `# python function to find maximum``# in arr[] of size n`  `def` `largest(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`  `# Driver Code``arr ``=` `[``10``, ``324``, ``45``, ``90``, ``9808``]``n ``=` `len``(arr)``Ans ``=` `largest(arr, n)``print``(``"Largest in given array "``, Ans)` `# This code is contributed by Smitha Dinesh Semwal`

Output

`Largest in given array  9808`

Time Complexity: O(N)
Auxiliary Space: O(1)

Approach 2 (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

 `# Python3 program to find maximum``# in arr[] of size n``def` `largest(arr, n):``    ``ans ``=` `max``(arr)``    ``return` `ans;` `# Driver code``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)

Approach 3 (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

 `# Python3 program to find maximum``# in arr[] of size n` `def` `largest(arr, n):` `    ``# Sort the array``    ``arr.sort()` `    ``# The last element of the``    ``# array is the largest element``    ``return` `arr[n``-``1``]``    ``# or return arr[-1]` `# Driver Code``arr ``=` `[``10``, ``324``, ``45``, ``90``, ``9808``]``n ``=` `len``(arr)``Ans ``=` `largest(arr, n)``print``(``"Largest in given array "``, Ans)` `# This code is contributed by Induri Deepthi`

Output

`Largest in given array  9808`

Approach 4 (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

 `# Python3 program to find maximum``# in arr[] of size n``from` `functools ``import` `reduce`  `def` `largest(arr):` `    ``# Sort the array``    ``ans ``=` `reduce``(``max``, arr)` `    ``return` `ans``    ``# or returning largest value`  `# Driver Code``arr ``=` `[``10``, ``324``, ``45``, ``90``, ``9808``]``n ``=` `len``(arr)``Ans ``=` `largest(arr)``print``(``"Largest in given array "``, Ans)` `# This code is contributed by Sam snehil`

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!

### Approach : using operator.gt()

Algorithm :

1. Create a array of integers and assign it to the variable arr.
2. Print the original array using the print() function.
3. Create an empty array and assign it to the variable arr1.
4. Sort the original array in descending order using the operator.gt() function and assign the sorted array to the variable arr1.
5. Print the biggest number in the array by accessing the first element of the sorted array using arr1.
6. Print a message indicating that the biggest number in the array has been found.

## Python3

 `# python program to find large number in the given array``import` `operator``# Initializing the list``arr ``=` `[``2``, ``1``, ``7``, ``3``, ``0``]``max``=``0``  ` `# printing the original list``print``(``'The given array is:'``, arr)` `#checking for large element``for` `i ``in` `arr:``  ``if` `operator.gt(i,``max``):``    ``max``=``i` `# printing the large number in the array``print``(``'The biggest number in the given array is:'``, ``max``)` `# This code is contributed by SHAIK HUSNA`

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)

My Personal Notes arrow_drop_up