Find the second largest number in an array in java or Find the second-highest number in java or Java 2nd, 3rd largest Number in array.

Many Interviews may Ask you to find the second highest Number in Array in java.
All know how to find the first highest number in an Array but some people don't know how to find the second-highest, third-highest number in array using Java. 
Here is Code How to Find Second or third or four highest Number in Array Using Java Code.

 public class FindHighestNumber {
  public static void main(String args[]) {
   int arr[] = { 98, 98, 51, 24, 45, 6, 70, 80, 80 };
   int firsthighest = 0;
   int secondhighest = 0;
   int thirdhighest = 0;

   for (int i = 0; i < arr.length; i++) {

    if (arr[i] > firsthighest) {
     secondhighest = firsthighest;
     firsthighest = arr[i];

    } else if (arr[i] > secondhighest && firsthighest > arr[i]) {
     // thirdhighest=secondhighest;
     secondhighest = arr[i];
    }
    /*
     * else if(arr[i]>thirdhighest && firsthighest>arr[i] && secondhighest>arr[i] ){
     * 
     * { thirdhighest=arr[i]; }
     */
   }
   System.err.println(firsthighest);

   System.err.println(secondhighest);

   // System.err.println(thirdhighest);
  }
 }
second highest number, largest number in array
Second-highest number 

Simple and easy way to find the second third highest value in an array. This question may be asked bu the interviewer in an interview on how to find the second third element in an array and you can write this simple program. 
Previous Post Next Post