Java Program to Find Largest Number in an Array

In this article, we are going to understand and perform a simple task in Java, in which, we are given an array, and we need to find the largest number in the array. We are required to write a Java program for the same. So, we will first understand the logic behind the program, and then we would try to implement the program.

Java Program to Find Largest Number in an Array

In order to completely understand the program, one needs to be familiar with some different concepts –

  • Array in Java
  • Loops in Java

Have a look at the below input-output examples, which would give us a clear idea about our objective.

Example Input Output –

Input array: {34, 23, 5, 4, 34, 45, 78, 55}
Output: The largest number in the array is: 78

Input array: {5,4, 23, 1, 77, 65, 99}
Output: The largest number in the array is: 99

So, from the input-output, the objective becomes more clear. There can be different methods through which, you can achieve your goal, and here, we are going to understand some simple methods of finding the largest number in the array in Java.

Way 1 – Sorting the array

We have an array, and we need to find the largest number in the array. So, in this simple method, all we have to do is to sort the array in ascending order, and the largest number in the array is going to be the last element in the sorted array. To sort the array, we can use the sort method from the Arrays class in Java.

Have a look at a simple program, in which, we try to sort the array in ascending order, and then get the last element of the sorted array, as the largest number in the array.

import java.util.Arrays;
public class Main
{
    public static void main(String[] args) {
    int[] arr = new int[] {5,4, 23, 1, 77, 65, 99};
    // sorting the array.
    Arrays.sort(arr);
    // the last element in the array is the largest number.
    System.out.println(“The largest number in the array is:” + arr[arr.length – 1]);
    }
}

As you can see in the above program, we are given an array, and we just need to get the largest number in the array. For that, we are just sorting the array in ascending order, and in the sorted array, the largest number is the last element in the array. You can try executing the above program on your own and try changing the values in the array to get some different outputs. Let’s try to have a look at the output of the above program now –

Output

The largest number in the array is:99

As you can see, we were able to get the largest number in the array, by easily sorting the array in ascending order. We have used the sort method from the Arrays class here, but you can also sort it manually.

Way 2 – Using an iterative approach

In this approach, we are going to iterate through the array, finding the largest number in the array. So, here is the set of steps that we are going to follow, to achieve our goal using this approach –

Assume that the first element in the array is the largest number in the given array
Iterate through the array, through each element, and if the current element in the array is greater than the assumed largest number, then we need to update our largest number since we found a number larger than the largest.

After we are done with the iteration, we just need to print the largest element as our output.

So, now let’s have a look at the Java program, which tries to demonstrate the same thing.

import java.util.Arrays;
public class Main
{
    public static void main(String[] args) {
    int[] arr = new int[] {34, 23, 5, 4, 34, 45, 78, 55};
    // assuming that the first element is the largest.
    int largest = arr[0];
    for (int i=0;i<arr.length ;i++) 
    {
        if(arr[i] > largest)
        largest = arr[i];
    }
    System.out.println(“The largest element in the array is:” + largest);
    }
}

As you can see in the above program, we are having an array, and then we are assuming that the first element in the array is the largest number in the array(also we assume that the array is non-empty). After that, we are looping through the array, and check if the current element in the array is larger than the assumed largest element, and if the condition is true, then we are just updating our largest number.

After we are done looping, we have the largest number in the array. So, we just need to show the largest number as our output for the program.

Let’s have a look at the output for the above program –

Output –

The largest element in the array is:78

As you can see, we have got the largest number in the array. You can try executing the above program and try playing with the numbers in the array, to get some different outputs.

Way 3 – Using the enhanced for a loop –

This approach is quite similar to the previous one, since here as well, we are going to loop. But here, we are going to make use of the enhanced for loop(also referred to as for-each loop). As mentioned earlier, this approach is much similar to the previous one, so here is the set of steps that we are going to follow, to achieve our goal –

import java.util.Arrays;
public class Main
{
    public static void main(String[] args) {
    int[] arr = new int[] {34, 23, 5, 4, 34, 45, 78, 55};
    // assuming that the first element is the largest.
    int largest = arr[0];
    for(int number : arr)
    {
        if (number>largest)
        largest = number;
    }
    System.out.println(“The largest element in the array is:” + largest);
    }
}

As you can see in the above program, we have an array, and then we are assuming that the first element in the array is the largest number in the array(also we are assuming that the array is non-empty). After that, we are looping, and check if the current number in the array is greater than the assumed largest and if yes, we are updating our largest number.

After we are out of the loop, we have our largest number stored in the variable largest, and we just need to show the largest number as our output.

Let’s have a look at the output of the above program –

Output –

The largest element in the array is:78

As you can see, using the enhanced for loop, we were able to get the largest number in the array.

Conclusion

In this article, we have seen some different methods to get the largest number in the array, and we performed Java programs for the same. There can be some other ways to achieve the same goal, but here, we have seen some simple methods, through which, we can get the largest number in the array, in Java.

FAQ About Java Program to Find Largest Number in an Array

Q: What is a simple method to find the largest number in the array?

Ans: To get the largest number in the array, you can simply sort the array in ascending order, and you would find the largest number as the last element in the array.

Q: What is a simple method to find the smallest number in the array?

Ans: To get the smallest element in the array, we just need to sort the array in ascending order, and we can find that the smallest number in the array is the first element in the array.