Find second largest number in array Java
In this post, you will learn how to find the largest of two numbers using the Java programming language. Such a type of programming can improve the thinking power of a new developer to tackle a given issue. There are different ways to find the largest of the two numbers. Here, we have mentioned most of them.
Find the second largest number using for loop
In the given Java program, we use a for loop to find the second largest number.
public class SecondLargestInArray{
public static int getSecondLargest(int[] a, int len){
int temp;
for (int i = 0; i < len; i++)
{
for (int j = i + 1; j < len; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[len-2];
}
public static void main(String args[]){
int a[]={22,42,12,14,92,17};
int b[]={12,14,72,21};
System.out.println("Second Largest Number: "+getSecondLargest(a,6));
System.out.println("Second Largest Number: "+getSecondLargest(b,4));
}
}
Output of the above code:
Second Largest Number: 42
Second Largest Number: 21
Find the second largest number using Arrays
In this post, you will learn how to find the second largest number using the Java arrays.
import java.util.Arrays;
public class SecondLargestInArray{
public static int getSecondLargest(int[] a, int len){
Arrays.sort(a);
return a[len-2];
}
public static void main(String args[]){
int x[]={32,84,48,91};
int y[]={2,4,5,7,1,6,9};
System.out.println("Second Largest Number: "+getSecondLargest(x,4));
System.out.println("Second Largest Number: "+getSecondLargest(y,7));
}
}
Output of the above code:
Second Largest Number: 84
Second Largest Number: 7
Find the second largest number using Collections
A collection represents a group of objects, known as its elements. Collections is a utility class that resides in the java.util package, it consists entirely of static methods which are used to operate on collections. In the given Java program, you find the second largest number using Collections.
import java.util.*;
public class SecondLargestInArray{
public static int getSecondLargest(Integer[] a, int len){
List list=Arrays.asList(a);
Collections.sort(list);
int element=list.get(len-2);
return element;
}
public static void main(String args[]){
Integer x[]={2,7,4,4,6,1};
Integer y[]={64,23,13,53,26};
System.out.println("Second Largest Number: "+getSecondLargest(x,6));
System.out.println("Second Largest Number: "+getSecondLargest(y,5));
}
}
Output of the above code:
Second Largest Number: 6
Second Largest Number: 53
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java