write java code to print second max number in the array

Answers were Sorted based on User's Feedback



write java code to print second max number in the array..

Answer / rajaram

public static void main(String[] args){
int maxNumber = 0;
int secondMaxNumber = 0;
if(args.length == 0){
System.err.println("Number array is empty");
return;
}
for(int i=0; i < args.length; i++){
int currNumber = Integer.parseInt(args[i]);
if(maxNumber < currNumber){
secondMaxNumber = maxNumber;
maxNumber = currNumber;
}else if(secondMaxNumber < currNumber){
secondMaxNumber = currNumber;
}
}
System.err.println("Max. number is "+maxNumber);
System.err.println("Second Max. is "+secondMaxNumber);
}
}

Is This Answer Correct ?    28 Yes 8 No

write java code to print second max number in the array..

Answer / himesh mistry

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;


public class SecondMax {

/**
* @param args
*/
public static void main(String[] args) {
int[] numbers = {9,4,8,0,0,5,9,1,4,2};
Set arrSet = new HashSet();
for (int i=0;i<numbers.length;i++) {
arrSet.add(numbers[i]);
}
ArrayList s = new ArrayList(arrSet);
Collections.sort(s);

System.out.println("Second element : " +
s.get(s.size()-2));
}
}

Is This Answer Correct ?    16 Yes 5 No

write java code to print second max number in the array..

Answer / geetha

Himesh Mistry -best solution

Is This Answer Correct ?    9 Yes 2 No

write java code to print second max number in the array..

Answer / sunny

I think, the except for Himesh's program, all other ones
doesnt handle duplicate values ...
I mean if the numbers are :- 9 9 8 8 7 7 7 1 3 2
the answer should be 8 ideally.
But I doubt whether thats really happening in any other
programs, except for Himesh's.
Do let me know, if I am wrong. Thanks.

Is This Answer Correct ?    5 Yes 0 No

write java code to print second max number in the array..

Answer / rajesh s

This is I tried.
public class SecondLargestNumber {
/**
* @param args
*/
public static void main(String[] args) {
int[] num = new int[]{-101,-105,-2,-7,-22,-
104,-8,-10,-100,-102,-102};
int big=-1;
int secbig=-1;
if(num.length == 1) {
big = num[0];
secbig = num[0];
} else {
if(num[0] > num[1]) {
big = num[0];
secbig = num[1];
} else {
big = num[1];
secbig = num[0];
}
}
if(num.length > 2){
for(int i=2;i<num.length;++i){
if(num[i] > secbig && num
[i] <big) {
secbig = num[i];
}
if(num[i]>=big){
secbig = big;
big = num[i];
}
}
}
System.out.println(big);
System.out.println(secbig);
}
}

Please check is it working and let me know. For removing
duplicates, we will have one more method to remove the
duplicate elements.

Is This Answer Correct ?    4 Yes 0 No

write java code to print second max number in the array..

Answer / raja ram

public class SecondMaximumNumber{
public static void main(String[] args){
int maxNumber = 0;
int secondMaxNumber = 0;
if(args.length == 0){
System.err.println("Number array is empty");
return;
}
for(int i=0; i < args.length; i++){
int currNumber = Integer.parseInt(args[i]);
if(maxNumber < currNumber){
secondMaxNumber = maxNumber;
maxNumber = currNumber;
}else if(secondMaxNumber < currNumber){
secondMaxNumber = currNumber;
}
}
System.err.println("Max. number is "+maxNumber);
System.err.println("Second Max. number is
"+secondMaxNumber);
}
}

Is This Answer Correct ?    7 Yes 5 No

write java code to print second max number in the array..

Answer / hitman

hey using collections solving this type of problem becomes
easy but whenever u go for interview most of the time they
ask you to write program without using collection API thts
why i think rajaram's code is correct

Is This Answer Correct ?    2 Yes 0 No

write java code to print second max number in the array..

Answer / ibrahim

import java.util.ArrayList;
public class maxSec {

public static void main(String[] args) {
int maxFi=0;
int maxSe=0;
ArrayList<Integer> arl=new ArrayList<Integer>();

arl.add(4); arl.add(8); arl.add(2);
arl.add(6); arl.add(7); arl.add(8);

for(int i=0; i<arl.size(); i++){
if (maxFi< arl.get(i))
maxFi= arl.get(i);
}

for(int i=0; i<arl.size(); i++){
if (maxSe <arl.get(i)){
if (arl.get(i)<maxFi ){

maxSe=arl.get(i);
}
}
}
System.out.println(maxFi);
System.out.println(maxSe);

}

}

Is This Answer Correct ?    0 Yes 0 No

write java code to print second max number in the array..

Answer / laddu

import java.util.Arrays;
import java.util.Collections;
import java.util.*;
public class Main1 {
public static void main(String[] args) {
Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};
int min = (int) Collections.min(Arrays.asList(numbers));
int max = (int) Collections.max(Arrays.asList(numbers));

ArrayList s = new ArrayList(Arrays.asList(numbers));
Collections.sort(s);

System.out.println("Second element : " + s.get(s.size()-2));
}
}

Is This Answer Correct ?    0 Yes 0 No

write java code to print second max number in the array..

Answer / sujeev

Ranjaram is correct.I modified and testes it.
public class test123 {
public static void main(String[] args){
int maxNumber = 0;
int secondMaxNumber = 0;

int[] anArray;
anArray =new int [10];
anArray[0] = 100;
anArray[1] = 200;
anArray[2] = 300;
anArray[3] = 400;
anArray[4] = 500;

if(anArray.length == 0){
System.err.println("Number array is empty");
return;
}
for(int i=0; i < anArray.length; i++){
int currNumber = anArray[i];
if(maxNumber < currNumber){
secondMaxNumber = maxNumber;
maxNumber = currNumber;
}else if(secondMaxNumber < currNumber){
secondMaxNumber = currNumber;
}
}
System.err.println("Max. number is "+maxNumber);
System.err.println("Second Max.
is "+secondMaxNumber);
}
}

Is This Answer Correct ?    2 Yes 3 No

Post New Answer

More Core Java Interview Questions

What things should be kept in mind while creating your own exceptions in java?

0 Answers  


Can you explain inner class.

0 Answers  


What are the 6 boolean operators?

0 Answers  


What language is java written?

0 Answers  


Is 0 true or is 1 true?

0 Answers  


What are the differences between checked exception and unchecked exception?

0 Answers  


IN java collections we have both interfaces and classes. instead of using interfaces why we can't use classes only like that why we can't use interfaces only. why we need two things interface and class.

6 Answers   Accenture, CTS,


Can we make main() thread as daemon?

0 Answers  


What are heterogeneous objects?

0 Answers  


what is bytecode? watz the difference between machine code and bytecode?

9 Answers   Oracle,


Can we extend the String class?

3 Answers   Truworth,


How do you find the maximum number from an array without comparing and sorting?

0 Answers   BlackRock,


Categories