crackyourinterview.com


To improves our performance please Like Share Subscribe(Will boost us)

Write code to find the Second Highest number in an ArrayList in Java?
Question Posted on 07 Apr 2021Home >> Java >> Core Java >> Write code to find the Second Highest number in an ArrayList in Java?

Write code to find the Second Highest number in an ArrayList in Java?
Below is the code to get the Higest and the second higest number from given array here i have created a class for that.

public class SecondHigest
{
  public static void main(String[] args)
  {
   int array[] = { 1, 2, 3, 4, 5 ,6 ,7 ,8 ,9 ,10 ,11 ,12, 13, 14};
   int highNumber = 0;
   int secondHighNumber = 0;
   for (int i = 0; i < array.length; i++)
   {
    if (array[i] > highNumber)
    {
     secondHighNumber = highNumber;
     highNumber = array[i];
    }
    else if (array[i] > secondHighNumber)
    {
     secondHighNumber = array[i];
    }
   }
   System.out.println("\nSecondHighest Number is:" + secondHighNumber);
   System.out.println("Highest Number is: " +highNumber);
  }
}
Output:

SecondHighest Number is:13

Highest number is: 14
0
0



.


Most Visited Questions:-

Deep Learning Questions Answers
Below are the different Deep Leaning Questions and answer a More...

Continuous Integration Questions Answers
Below are the 20 odd questions for CI or Continuous Integra More...

Derived relationships in Association Rule Mining are represented in the form of __________.
Derived relationships in Association Rule Mining are repres More...

What is Gulpjs and some multiple choice questions on Gulp
Gulpjs is an open source whihc helps in building Javascript More...

Microservices Architecture Questions Answers
Below are the different questions on Microservices Architec More...




Other Important Questions

what is an object

what is OOAD

difference between c++ and java

what is a transient variable in java

difference between C and Java on pointers






@2014-2022 Crackyourinterview (All rights reserved)
Privacy Policy - Disclaimer - Sitemap