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) {
set.add(i);
}
// Remove the maximum value; print the largest remaining item
set.remove(set.last());
return set.last();
}
}
}
Output looks like:
SortedSet = second--->8
Happy Coding!!!
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) {
set.add(i);
}
// Remove the maximum value; print the largest remaining item
set.remove(set.last());
return set.last();
}
}
}
Output looks like:
SortedSet = second--->8
Happy Coding!!!