write java code to print second max number in the array
Answer Posted / 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 |
Post New Answer View All Answers
do I need to use synchronized on setvalue(int)? : Java thread
What is string intern in java?
How do you convert an int to a string in java?
What is the difference between Grid and Gridbaglayout?
What is finally block?
why doesn't java run on all platforms?
What is consumer in java?
Differentiate jar and war files?
What is a copy constructor in java?
Will the jvm load the package twice at runtime?
Why super is first line in java?
Compare java and python.
What is the purpose of the return statement?
What is difference between static class and normal class?
Tell me about your ability to work under pressure