Given an array
arr of size
n, write a program to find the largest element in it.
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.
- Take an array A and define its values
- Declare largest as integer
- Set ‘largest’ to 0
- Loop for each value of A
- If A[n] > largest, Assign A[n] to largest
- After loop finishes, Display largest as largest element of array