Java Program to find largest element in an array
Given an array, find the largest element in it.
Input : arr[] = {10, 20, 4} Output : 20 Input : arr[] = {20, 10, 20, 4, 100} Output : 100
Method 1: Iterative Way
Java
// Java Program to find maximum in arr[] class Test { static int arr[] = { 10 , 324 , 45 , 90 , 9808 }; // Method to find maximum in arr[] static int largest() { int i; // Initialize maximum element int max = arr[ 0 ]; // Traverse array elements from second and // compare every element with current max for (i = 1 ; i < arr.length; i++) if (arr[i] > max) max = arr[i]; return max; } // Driver method public static void main(String[] args) { System.out.println( "Largest in given array is " + largest()); } } |
Largest in given array is 9808
Output:
Largest in given array is 9808
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.
Method 2: Java 8 Stream You can simply use the new Java 8 Streams but you have to work with int.
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); } } |
Largest in given array is 9808
Output:
Largest in given array is 9808
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.
Method 3 : (Sorting)
Java
// Java program to find maximum in // arr[] of size n import java .io.*; import java.util.*; class GFG { // returns maximum in arr[] of size n static int largest( int []arr, int n) { Arrays.sort(arr); return arr[n - 1 ]; } // Driver code static public void main (String[] args) { int []arr = { 10 , 324 , 45 , 90 , 9808 }; int n = arr.length; System.out.println(largest(arr, n)); } } |
9808
Alternate way to write same Program without using any user defined function :
Java
/*package whatever //do not write package name here */ 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 ]); } } |
Largest number from given array: 3456
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.
Please refer complete article on Program to find largest element in an array for more details!
Method 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.
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)); } } |
Largest in given array is 9808
Please Login to comment...