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
What are the parts of a method?
What are aggregate functions explain with examples?
What is argument in java?
What do you mean by Hash Map and Hash Table?
What is var keyword ?
How does callback work in java?
Which methods are used during serialization and deserialization process?
Can a lock be acquired on a class in java programming?
What's the base class of all exception classes?
What is io stream in java?
What is the driver class?
If an object reference is set to null, will the garbage collector immediately free the memory held by that object?
What is number data type?
What are the new features in java 8?
What is %02d?