Image

Programs - Python - Arrays

Program:


# Initialize array
arr = [25, 11, 7, 75, 56]

# Initialize max with first element of array.
large = arr[0]

# Loop through the array
for i in range(0, len(arr)):
# Compare elements of array with max
if arr[i] > large:
large = arr[i]

print("Largest element present in given array: ", str(large))


# Alternative solution with built-in max() method
arr = [25, 11, 7, 75, 56]
print("Largest element present in given array: ", max(arr))

Output:

Largest element present in given array: 75