Display Second Largest number in java

Displaying second largest number in string array in java.

Here is the full source code, how to display second largest number in integer array ,
public class One {
public static void main(String[] args) {

        int arr[] = {5,8,0,7,12};
        System.out.println("SortedSet = second largest number is--->"+displaySecond(arr));

private static int displaySecond(int[] arr1) {
if (arr1.length==0 || arr1.length==1) {
        return -1;
        }else {
        SortedSet<Integer> set = new TreeSet<Integer>();
        for (int i: arr1) {
        // Remove the maximum value; print the largest remaining item
        return set.last();

Output looks like:

SortedSet = second--->8

Happy Coding!!!

No comments:

Post a Comment


Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace