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
Which is better stringbuilder or stringbuffer?
How does list work in java?
What is consumer interface?
If an object is garbage collected, can it become reachable again?
What are java packages? What is the significance of packages?
Discuss different types of errors that generally occur while programming.
what is instanceof operator used in java?
What is scanner in java?
How do you download stubs from Remote place?
Does the order of public and static declaration matter in main method?
What is the difference between the prefix and postfix forms of the ++ operator?
How do you represent a space in regex java?
What do you understand by the term string pool?
What is t type java?
What are the different access modifiers available in java?