Write a program to find the largest element in an Array?

Given an array arr[] of size n, write a program to find the largest element in it.

Example 1:

Input: arr[] = [100, 50, 4] Output: 100 Explanation: 100 is the largest element in the given array.

We accept the array elements from the user and run a for loop to check whether the next element in the unsorted array are of higher value compared to the previous elements.

If yes, we switch the element. We continue this algorithm until we reach the end of the array and store the max value in a variable which is outputted at the end.

Psuedo Code:

  1. Take an array A and define its values
  2. Declare largest as integer
  3. Set ‘largest’ to 0
  4. Loop for each value of A
  5. If A[n] > largest, Assign A[n] to largest
  6. After loop finishes, Display largest as largest element of array