Posts

Showing posts with the label Largest Element in an Array

Java - Program to Find Largest Element in an Array

In this section, we will  show you how to find the largest element in an Array. Using Iterative Method Using  Arrays.sort()  Method 1. Using Iterative Method public class Main { public static void main ( final String [] args) { final int [] array = { 1 , 4 , 44 , 5 , 66 , 2 }; largestElement ( array ); } private static int largestElement ( final int [] array){ // Initialize maximum element int max = array[ 0 ]; // Traverse array elements from second and // compare every element with current max for ( int i = 1 ; i < array. length ; i ++){ if (array[ i ] > max ){ max = array[ i ]; } } System . out .println( max ); return max ; } } Console Output: 66 2. Using Arrays.sort() Method import java.util.Arrays ; public class Main { public static void main ( final String [] args) { final int [] array = { 1 , 4 , 44 , 5 , 66 , 2 }; final