Python program for largest Element In an Array
In this tutorial, we will learn how to find and display the largest element in an array using the Python programing language. We will be using the concept of loop and conditional statements in this tutorial.
Python arrays are data structures that store homogenous data. Arrays stores objects of the same datatype. Arrays are mutable, meaning that the data in an array can be changed and iterative, meaning each element in an array can be accessed one by one.
In python, you need to import the array module to create an array. The array(data_type, value) function is used to specify an array with its data type and value list given as arguments. Look at the example given below for creating an array in Python.
import array as ar
a = ar.array('i', [1, 2, 3, 4, 5])
Here we have declared an array of integer type with the value list [1, 2, 3, 4, 5]. The letter i is a type code. This determines the type of the array during creation.
Some commonly used codes are-
Code |
Python Type |
i |
integer |
u |
unicode |
f |
float |
l |
integer |
Look at the sample input and output:
Input- arr[] = {50,12,23,11}
Output- 50
Approach to find largest Element In an Array
To calculate the largest element in an array we can follow two methods-
- By using max()
- by using loop
Approach 1: By using max() function
In this approach, we have used a built-in function max() that is predefined in the Python library and returns the largest element in an array.
Algorithm
Step 1- Import array module
Step 2- Declare array using the array module
Step 3- Call max() function
Step 4- Print the result
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach.
import array as arr
#input values
a = arr.array('i',[3, 24, 11, 2])
# display
print (' Greatest element in the array is ', max(a) )
Greatest element in the array is 24
Approach 2: Using loop and condition
In this approach, we will use a loop that will run from the starting to the ending index of the array and compare each element to find the greatest value. For checking each element we will use the if conditional statement.
Algorithm
Step 1- Import array module
Step 2- Define a function to find the greatest element in the array
Step 3- Declare a variable to store the maximum element and set its value as the first element of the array
Step 4- Calculate the length of the array using len() function
Step 5 - Run a loop for all the elements in the array
Step 6- Compare each element, if the value of the array element is greater than the value stored in the max variable, update the value of max and store the current array element
Step 7- Return max
Python Program 2
Look at the program to understand the implementation of the above-mentioned approach.
import array as ar
def MaxofArray(arr):
max=a[0]
n = len(arr)
for i in range(n):
if(max<a[i]):
max=a[i]
return max
#input values to list
a = ar.array('i',[10, 21, 12, 13])
# display sum
print ('Max of the array is ', MaxofArray(a) )
Max of the array is 21
Conclusion
In this tutorial, we have learned, two approaches by which we can find the greatest element in an array given by the user. One, by using a predefined function max() in the Python Library which will return the maximum of the elements. Second, by using a loop to compare each element and find the greatest value.