Home >>Java Programs >Java Program to find Third Largest Number in an Array
In this example, we will create a java program to find the third-largest number in an array.
We can achieve this by sorting the given array and returning the third largest number of that array.
We can achieve this by sorting the given array and returning the third largest number of that array.
Program:
public class Main
{
public static int getThirdLargest(int[] a, int total)
{
int temp;
for (int i = 0; i < total; i++)
{
for (int j = i + 1; j < total; j++)
{
if (a[i] > a[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
return a[total-3];
}
public static void main(String args[]){
int a[]={1,2,7,9,3,6};
int b[]={11,66,89,24,33,91,82,77};
System.out.println("Third Largest: "+getThirdLargest(a,6));
System.out.println("Third Largest: "+getThirdLargest(b,8));
}}