The Artima Developer Community
Sponsored Link

Java Buzz Forum
Find top two maximum numbers in a array java

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Find top two maximum numbers in a array java Posted: Aug 5, 2016 11:48 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Find top two maximum numbers in a array java
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Hi Friends today we will discuss about how to find top two maximum numbers in an array using java program.
  • For this we have written separate function to perform logic
  • findTwoMaxNumbers method takes integer  array as an argument
  • Initially take two variables to find top to numbers and assign to zero.
  • By using for each loop iterating array and compare current value with these values
  • If our value is less than current array value then assign current value to max1 
  • And assign maxone to maxtwo because maxtwo should be second highest.
  • After completion of all iterations maxone will have top value and maxtwo will have second maximum value.
  • Print first maximum and second maximum values.
  • So from main method create array and pass to findTwoMaxNumbers(int [] ar).


Program #1: Java interview programs to practice: find top two maximum numbers in an array without recursion 

  1. package arraysInterviewPrograms.instanceofjava;
  2. public class FindTopTwo {
  3.  
  4. public void findTwoMaxNumbers(int[] array){
  5.        
  6.  int maxOne = 0;
  7.  int maxTwo = 0;
  8.  
  9. for(int i:array){
  10.  
  11.     if(maxOne < i){
  12.            maxTwo = maxOne;
  13.            maxOne =i;
  14.      } else if(maxTwo < i){
  15.                 maxTwo = i;
  16.      }
  17. }
  18.         
  19.  
  20.   System.out.println("First Maximum Number: "+maxOne);
  21.   System.out.println("Second Maximum Number: "+maxTwo);
  22. }
  23.      
  24. public static void main(String a[]){
  25.  
  26.         int num[] = {4,23,67,1,76,1,98,13};
  27.         FindTopTwo obj = new FindTopTwo();
  28.         obj.findTwoMaxNumbers(num);
  29.         obj.findTwoMaxNumbers(new int[]{4,5,6,90,1});
  30.  
  31. }
  32.  
  33. }


Output:


  1. First Maximum Number: 98
  2. Second Maximum Number: 76
  3. First Maximum Number: 90
  4. Second Maximum Number: 6

Program #2:Java program to find top two maximum numbers in an array using eclipse IDE

top two maximum number in array java


Read: Find top two maximum numbers in a array java

Topic: How to run jsp program in eclipse tomcat Previous Topic   Next Topic Topic: Big Data SQL: Overview of Apache Drill Query Execution Capabilities – Whiteboard Walkthrough

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use