Find The Largest And Second Largest Element of an Array in Java

In this post we’ll see a Java program to find the largest and second largest element of an array without using any inbuilt Java method.

Steps to find the largest and second largest element of an array

  1. Declare two variables (first and second) initialized with value as 0.
  2. Iterate the array and compare the current array element with variable first. If element is greater than the first then assign existing value of first to second and element to first.
  3. If current array element is less than the first then also compare element with second. If element is greater than the second then assign element to second.

Largest and second largest element of an array-Java program

Output

That’s all for the topic Find The Largest And Second Largest Element of an Array in Java. If something is missing or you have something to share about the topic please write a 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.