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
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 package arraysInterviewPrograms.instanceofjava; public class FindTopTwo { public void findTwoMaxNumbers(int[] array){ int maxOne = 0; int maxTwo = 0; for(int i:array){ if(maxOne < i){ maxTwo = maxOne; maxOne =i; } else if(maxTwo < i){ maxTwo = i; } } System.out.println("First Maximum Number: "+maxOne); System.out.println("Second Maximum Number: "+maxTwo); } public static void main(String a[]){ int num[] = {4,23,67,1,76,1,98,13}; FindTopTwo obj = new FindTopTwo(); obj.findTwoMaxNumbers(num); obj.findTwoMaxNumbers(new int[]{4,5,6,90,1}); } } Output: First Maximum Number: 98 Second Maximum Number: 76 First Maximum Number: 90 Second Maximum Number: 6 Program #2:Java program to find top two maximum numbers in an array using eclipse IDE
Read: Find top two maximum numbers in a array java