• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

SoftwareTestingo - Interview Questions, Tutorial & Test Cases Template Examples

  • Home
  • Interview Questions
  • Java
  • Java Programs
  • Selenium
  • Selenium Programs
  • Manual Testing
  • Test Cases
  • Difference
  • Tools
  • SQL
  • Contact Us
  • Search
SoftwareTestingo » Java » Java Programs » Write a Program to Get Second Highest Element In an Array

Write a Program to Get Second Highest Element In an Array

Last Updated on: November 3, 2018 By Softwaretestingo Editorial Board

What We Are Learn On This Post

  • Write a Program to Get Second Highest Element In an Array:

Write a Program to Get Second Highest Element In an Array:

package com.java.ArrayEx;
public class Array2ndLargestvalue {
   public static void main(String[] args) 
   {
      int numbers[] = { 47, 3, 37, 12, 46, 5, 64, 21 };
      //System.out.println(numbers.length);
      int highest = 0;
      int second_highest = 0;
      for (int n : numbers) {
         if (highest < n) {
            second_highest = highest;
            highest = n;
         } else if (second_highest < n) {
            second_highest = n;
         }
      }
      System.out.println("First Max Number: " + highest);
      System.out.println("Second Max Number: " + second_highest);
   }
}
Check Also: Program To Find The Duplicate Elements

The Final Output:

First Max Number: 64
Second Max Number: 47

    Filed Under: Java Programs

    Reader Interactions

    Leave a Reply Cancel reply

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

    Primary Sidebar

    Join SoftwareTestingo Telegram Group

    Categories

    Copyright © 2022 SoftwareTestingo.com ~ Contact Us ~ Sitemap ~ Privacy Policy ~ Testing Careers