Find largest and second largest number in the given Array

By | December 16, 2018

In post of Find largest and second largest number in the given Array 
Condition here is that you should not be using any inbuilt Java classes or methods (i.e. Arrays.sort) or any data structure.

Logic to find largest and second largest number in an array

In this logic we have two variables for first and second number and iterate the array. After that we compare each array element with the first number if first number is less than the array element then assign existing first number to second number and array element to the first number.

You may also like –
Find largest and smallest number in the given Array
Find duplicate elements in an Array
Java program to Remove Duplicate Elements From an Array

Core Logic here we have If first number is greater than the array element then check if second element is less than the array element, if yes then assign array element to the second number.

Output Of Program:

That’s all about this topic Find largest and second largest number in the given Array. If you have any doubts or any suggestions to make please drop a comment. 

Leave a Reply

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