# Java Program To Find the Largest Element in an Array

In this tutorial, we will learn how to find the 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 at the 0th 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: Largest = 9

## Program 1: Find the Largest Element in an Array

In this approach, we will directly find the largest element in the main method itself. Firstly, we will sort the elements in descending order then will return the element stored in the 0th index.

### Algorithm

1. Start
2. Declare an array.
3. Initialize the array.
4. Use two for loops to display the largest element in an array.
5. Use the first for loop to hold each element of the array
6. Use the second for loop to compare the element with the rest of the elements.
7. Swap the elements to sort the elements.
8. Display the largest element.
9. Stop

Below is the code for the same.

The below program demonstrates how to directly find the largest element in an array. Firstly, an array is declared and then initialized. With the help of two for loops, all the elements of the array are iterated and then the elements are compared and swapped in descending order. Then display the largest element in the array.

``````/*Java Program to find the largest element in an array without using Functions*/
import java.util.Scanner;

public class findElement
{
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();
}

for(int i=0;i<n;i++)   //Use to hold an element
{
for(int j=i+1;j<n;j++)   //Use to check for rest of the elements
{
if(arr[i]<arr[j])    //Compare and swap
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}

System.out.println("Largest element is "+arr);  //Display Largest

}
}``````

Enter the size of the array 10
Enter the array 87 6 7 5 4 3 4 32 12 1
Largest element is 87

## Program 2: Find the Largest Element in an Array

In this approach, we will use a separate method to find the largest element in the array using `Arrays.sort()`. The `Arrays.sort()` is present in java.util package. Arrays class automatically sorts the array when called. It reduces the work of the user as the user will not have to write a different code for sorting the array.

### Algorithm

1. Start
2. Declare an array.
3. Initialize the array.
4. Call a method that will display the largest element in the array.
5. Use `Arrays.sort()` to sort the elements of the array first.
6. The last element is the largest element in the array.
7. Display the last element.
8. Stop

Below is the code for the same.

The below program demonstrates how to use a separate method to find the largest element in an array. Firstly, an array is declared and then initialized. A method is called next. The array is then sorted using `Arrays.sort()` and the last index element is displayed i.e., the largest element is displayed in the array.

``````/*Java Program to find the largest element in an array using Arrays.sort()*/
import java.util.Scanner;
import java .io.*;
import java .util.*;

public class findElement
{
static void findLargest(int arr[], int n)    //Method to display the largest element
{

Arrays.sort(arr);    //Sort the array

System.out.println("Largest element is "+arr[n-1]);  //Display Largest Element

}

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 56 7 6 45 3 4 23 12 21 1
Largest element is 56