Java Program to Find Largest Element of an Array

Java Program to Find Largest Element of an Array

What is an Array in Java ?

An array in Java is a container object that holds a fixed number of values of the same type. Each value is called an element and can be accessed through an index, which is an integer value that represents its position in the array. Arrays are created with a specified size and can’t be resized dynamically.

Steps to Find Largest Element of an Array : 

  1. Declare an array: Declare an array with a specified size and initialize it with the values you want to store.
  2. Initialize a variable to store the maximum value: Before you start iterating through the array, initialize a variable to store the maximum value. You can set this variable to the smallest possible value for the data type of the array elements.
  3. Iterate through the array: Use a for loop to iterate through the array. For each iteration, compare the current element in the array with the current maximum value. If the current element is greater than the current maximum value, update the maximum value.
  4. Return the maximum value: After the for loop, the maximum value is stored in the maxValue variable. You can return this value or store it in a variable for later use.

Code example for the above algorithm :

public static int findLargest(int[] array) {
    int maxValue = Integer.MIN_VALUE;
    for (int i = 0; i < array.length; i++) { 
            if (array[i] > maxValue) {
            maxValue = array[i];
        }
    }
    return maxValue;
}

Example : Using a for loop 

Run

public class Main {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5};
        int largest = findLargest(array);
        System.out.println("The largest element in the array is: " + largest);
    }
 
    public static int findLargest(int[] array) {
        int maxValue = Integer.MIN_VALUE;
        for (int i = 0; i < array.length; i++) { if (array[i] > maxValue) {
                maxValue = array[i];
            }
        }
        return maxValue;
    }
}

Output :

The largest element in the array is: 5

Explanation : 

In this example, the main method declares an array of integers and calls the findLargest() function to find the largest element in the array. The result is then printed to the console. The findLargest() function implements the algorithm to find the largest element in an array as described in the above algorithm.

Example : Using while loop 

Run
public class Main {
    public static void main(String[] args) {
        int[] array = {10, 20, 30, 40, 50};
        int largest = findLargest(array);
        System.out.println("The largest element in the array is: " + largest);
    }
 
    public static int findLargest(int[] array) {
        int maxValue = Integer.MIN_VALUE;
        int i = 0;
        while (i < array.length) {
            if (array[i] > maxValue) {
                maxValue = array[i];
            }
            i++;
        }
        return maxValue;
    }
}

Output :

The largest element in the array is: 50

Explanation :  

In this example, the main method and the call to findLargest is the same as the previous example. The findLargest function has been modified to use a while loop instead of a for loop. The while loop initializes a variable i to 0 and continues to loop while i is less than the length of the array. The body of the loop compares the current element in the array to the current maxValue, updating maxValue if necessary. The loop increments the value of i at the end of each iteration. , and the largest element in the array returned by the findLargest function.

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription