Java Program to find largest element in an array
Last Updated :
11 Jan, 2024
Given an array, find the largest element in it.
Examples of Finding the largest element in an array
Input : arr[] = {10, 20, 4}Output : 20Input : arr[] = {20, 10, 20, 4, 100}Output : 100
Methods to Find the Largest Element in an Array
There are certain methods to find the Largest Element in an Array as mentioned below:
- Iterative Way
- Java 8 Stream
- Sorting
- Using Collections.max()
1. Iterative Method (Brute Force Method)
Below is the implementation of the above method:
Java
class Test
{
static int arr[] = { 10 , 324 , 45 , 90 , 9808 };
static int largest()
{
int i;
int max = arr[ 0 ];
for (i = 1 ; i < arr.length; i++)
if (arr[i] > max)
max = arr[i];
return max;
}
public static void main(String[] args)
{
System.out.println( "Largest in given array is " + largest());
}
}
|
Output
Largest in given array is 9808
Complexity of the above method:
Time Complexity: O(n), where n represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
2. Java 8 Stream
You can simply use the new Java 8 Streams but you have to work with int.
Below is the implementation of the above method:
Java
import java.util.Arrays;
public class GFG {
public static void main(String[] args){
int arr[] = { 10 , 324 , 45 , 90 , 9808 };
int max = Arrays.stream(arr).max().getAsInt();
System.out.println( "Largest in given array is " +max);
}
}
|
Output
Largest in given array is 9808
Complexity of the above method:
Time Complexity: O(n), where n represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
3. Sorting the Array
We can sort the array to get the largest element from the array. After sorting we can directly extract last element as largest element(Ascending order).
i). User Defined Method:
User Defined Method is where we can provide the sort function which will sort the array .
Note: Sorting Algorithm used in the Program mentioned below is Insertion Sort, However we can use alternative sorting algorithm too.
Below is the implementation of the above method:
Java
import java.io.*;
import java.util.*;
class Main{
static int largest( int arr[], int n)
{
for ( int i = 1 ; i < n; ++i) {
int key = arr[i];
int j = i - 1 ;
while (j >= 0 && arr[j] > key) {
arr[j + 1 ] = arr[j];
j = j - 1 ;
}
arr[j + 1 ] = key;
}
return arr[n- 1 ];
}
static public void main(String[] args)
{
int [] arr = { 10 , 324 , 45 , 90 , 9808 };
int n = arr.length;
System.out.println(largest(arr, n));
}
}
|
ii). Pre Defined Function
We can use sort function is a sorting algorithm.
Alternate way to write same Program with predefined function :
Java
import java.io.*;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int arr[] = { 12 , 45 , 67 , 89 , 100 , 23 ,
3456 , 897 , 452 , 444 , 899 , 700 };
Arrays.sort(arr);
System.out.println(
"Largest number from given array: "
+ arr[arr.length - 1 ]);
}
}
|
Output
Largest number from given array: 3456
Complexity of the above method:
Time Complexity: O(n logn), where n represents the size of the given array.
Auxiliary Space: O(1), no extra space is required, so it is a constant.
4. Using Collections.max()
Define an empty ArrayList and add all elements of array to it.Pass this ArrayList to Collections.max().The max() method of java.util.Collections class is used to return the maximum element of the given collection, according to the natural ordering of its elements.
Blow is the implementation of the above method:
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
int arr[] = { 10 , 324 , 45 , 90 , 9808 };
List<Integer> list = new ArrayList<>();
for ( int i = 0 ; i < arr.length; i++) {
list.add(arr[i]);
}
System.out.println( "Largest in given array is "
+ Collections.max(list));
}
}
|
Output
Largest in given array is 9808
Please refer complete article on Program to find largest element in an array for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...