Be the first user to complete this post
|
Add to List |
366. Java program to find the largest element in array
Objective- Given an array of numbers, write a java program to find the largest element in the given array.
Example:
int [] a = {1, 5, 3, 9, 2, 8, 2} Largest Element: 9
Approach: Linear Search
- Initialize a variable largest_element = a[0].
- Run a loop from 2nd element till the last element in the array.
- During iteration whenever find element which is greater than largest_element, update the largest_element with the current element.
- See the code and run on IDE for better understanding.
Output:
Largest element in array: 9