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.


int [] a = {1, 5, 3, 9, 2, 8, 2}
Largest Element: 9

Approach: Linear Search

  1. Initialize a variable largest_element = a[0].
  2. Run a loop from 2nd element till the last element in the array.
    1. During iteration whenever find element which is greater than largest_element, update the largest_element with the current element.
  3. See the code and run on IDE for better understanding.

Java Code:


Largest element in array: 9

Top Companies Interview Questions..-

Google Microsoft Amazon Facebook more..

If you find anything incorrect or you feel that there is any better approach to solve the above problem, please write comment.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: