Java Program To Find the Largest Two Numbers in A given array

In this tutorial, we will learn how to find the two largest elements in an array. The easiest way to find the two largest elements is by first sorting the elements and then returning the elements stored in the 0th and first index. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input: Enter the Array Elements: 7 6 9 2 4 1 3 6 9

Output: First largest = 9

Second Largest = 7

Program 1: To Find the two Largest Element in an Array

In this approach, we will directly find the largest and second-largest element in the array in the main method itself.

Algorithm

  1. Start
  2. Declare an array.
  3. Initialize the array.
  4. In the same main method, check for the largest and second-largest elements.
  5. Declare the two largest variables and initialize them with the first and second elements of the array.
  6. Then by swapping and comparing we find the largest and second-largest element.
  7. Again we check for the elements to avoid duplicate elements.
  8. Display the result.
  9. Stop

Below is the code for the same.

The below program demonstrates how to find the two largest numbers in an array without using Functions. In this program, firstly we declare and initialize the array. Then in the main method itself, we declare two variables and assign the first two elements as the largest and second-largest elements. Then, by swapping and comparing we find the elements and display the output.

/*Java Program to directly find the two largest elements in an array*/
import java.util.Scanner;

public class LargestElement
{

     public static void main(String []args)
     {
         Scanner sc=new Scanner(System.in);
         int n;     //Declare array size
         System.out.println("Enter the size of the array");
         n=sc.nextInt();   //Initialize array size
         
         int arr[]=new int[n];   //Declare array 
        System.out.println("Enter the array");  
        for(int i=0;i<n;i++)     //Initialize array
        {
            arr[i]=sc.nextInt();
        }
               
        int lar1=arr[0];   //Initialize the first largest element
        int lar2=arr[1];   //Initialize the second largest element

         //Check by swapping elements
        
               if(lar1<lar2)
                {
                    int temp=lar1;
                    lar1=lar2;
                    lar2=temp;
                }
                
        for(int i=2;i<n;i++)
        {
            if (arr[i] > lar1)
			{
				lar2 = lar1;
				lar1 = arr[i];
			}
			else if (arr[i] > lar2 && arr[i] != lar1)
			{
				lar2 = arr[i];
			}
        }
        System.out.println("First Largest"+lar1);    //Display the first largest
        System.out.println("Second Largest "+lar2);  //Display the second largest
        
       
    }
}


Enter the size of the array 7
Enter the array 1 5 6 2 3 4 6 6
First Largest6
Second Largest 5

Program 2: To Find the two Largest Element in an Array

In this approach, we will use a separate method to find the largest and second-largest element in the array using Arrays.sort() method.

Algorithm

  1. Start
  2. Declare an array.
  3. Initialize the array.
  4. Call a separate method that will display the first two largest elements in an array.
  5. Declare the two largest variables and initialize them with the first and second elements of the array.
  6. Then by using Array.sort() first sort the elements of the array.
  7. Check for the largest and second-largest elements.
  8. Again check for duplicate elements.
  9. Display the result.
  10. Stop

Below is the code for the same.

The below program demonstrates how to find the two largest numbers in an array using separate methods. In this program, firstly we declare and initialize the array. Then we call a method and declare two variables and assign the first two elements as the largest and second-largest elements. Then, by using Arrays.sort() first sort the array and then find the two largest elements and display the output.

/*Java Program to find the two largest elements in an array using separate methods*/
import java.util.Scanner;
import java.util.*;

public class LargestElement
{

     static void findLargest(int arr[], int n)      //Method to find the two largest element
     {
        int lar1=arr[0];     //Initialize the first largest
        int lar2=arr[1];     //Initialize the second largest
        
        //Sort the elements of the array using Array.sort()
               Arrays.sort(arr);
        for(int i=2;i<n;i++)
        {
            if (arr[i] > lar1)
			{
				lar2 = lar1;
				lar1 = arr[i];
			}
			else if (arr[i] > lar2 && arr[i] != lar1)
			{
				lar2 = arr[i];
			}
        }
        System.out.println("First Largest"+lar1);    //Display First Largest
        System.out.println("Second Largest "+lar2);  //Display Second  Largest
         
     }
     public static void main(String []args)
     {
         Scanner sc=new Scanner(System.in);
         int n;     //Declare array size
         System.out.println("Enter the size of the array");
         n=sc.nextInt();   //Initialize array size
         
         int arr[]=new int[n];   //Declare array 
        System.out.println("Enter the array");  
        for(int i=0;i<n;i++)     //Initialize array
        {
            arr[i]=sc.nextInt();
        }
               
        findLargest(arr,n);     
       
    }
}


Enter the size of the array 10
Enter the array 4 6 1 2 3 98 6 2 98 12
First Largest 98
Second Largest 12