Find the 3rd largest number in a Java array.

Java Programming Java8Object Oriented Programming

Example

 Following is the required program.

Live Demo

public class Tester {
   public static int getThirdLargest(int[] a) {
      int temp;
      //sort the array
      for (int i = 0; i < a.length; i++) {
         for (int j = i + 1; j < a.length; j++) {
            if (a[i] > a[j]) {
               temp = a[i];
               a[i] = a[j];
               a[j] = temp;
            }
         }
      }
      //return third largest element
      return a[a.length - 3];
   }
   public static void main(String args[]) {
      int a[] = { 11,10,4, 15, 16, 13, 2 };
      System.out.println("Third Largest: " +getThirdLargest(a));
   }
}

Output

Third largest: 13
raja
Published on 23-Apr-2018 12:58:09
Advertisements