Find the largest element in an array in python

In this article, we will learn to find the largest element from an array using the python program.

we find the largest element in an array of using in python using two methods

  • Using loop
  • Using max() function


Method 1: Using a loop

Explanation

  • First, we set the max value to the first element of the array.
  • Then, we iterate the loop and check the array element is greater than the max value, if it is max, then we update the max value to the greater value of an element of the array.
  • Then, lastly, we print the max value that's the largest element of an array

Program

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# find the largest element from the array
# arr
arr = [1,12,4,15,6,34,22]

# length of array
length = len(arr)

# max = first element of the arr
max = arr[0]

# now iterate the loop
for i in range(0,length):

    # check the element is greater than the max
    if arr[i]>max:
        # if it is greater than update the max value 
        max = arr[i]

# print the max
print(max)

# find the largest element from the array in python - docodehere.com

Output

34

Method 2: Using max() function

Explanation

We use the max() function to find the largest element in an array.

Program

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# find the largest element from the array
# arr
arr = [1,12,4,15,6,34,22]

# use max() function to find the maximum value 
maximum = max(arr)

# print the largest element 
print(maximum)

# find the largest element from the array in python - docodehere.com

Output

34
Next Post Previous Post
No Comment
Add Comment
comment url