Java – Find Largest Number of an Array

A number array like integer array, float array, double array or long array can contain numbers with different values. We can find the largest number of these, in an array.

In this tutorial, we shall learn how to find the largest number of a given array using different looping statements in Java.

Example 1 – Find Largest Number of Array using While Loop

In this example, we shall use Java While Loop, to find largest number of given integer array.

Solution

  1. Take an integer array with some elements.
  2. Initialize a variable largest with the lowest of the integer value, Integer.MIN_VALUE. This ensures that the largest picks the first element of the given array, in first iteration of the loop.
  3. Take a variable index. We shall use it to access elements of the array. Initialize index with zero. We shall start from the left of array.
  4. Write a while loop that executes when index is less than length of the integer array. Increment index during each iteration.
    1. Inside while loop, check if largest is less than this element. If so, then update the largest with this element. If largest is not less than this element, we are happy with existing value of largest, nothing to do, so no else block required.
  5. After the while loop execution is done, we end with the largest element of array, in the variable largest.

Java Program

</>
Copy
/**
 * Java Program - Find Largest Number of an Array
 */

public class LargestNumberArray {
	public static void main(String[] args) {
		//an array
		int[] arr = {25, 86, 41, 97, 22, 34};
		
		//initialize with smallest possible value
		int largest = Integer.MIN_VALUE;
		
		//find largest element of array
		int index = 0;
		while( index < arr.length ) {
			//check if largest is smaller than element
			if( largest < arr[index] ) {
				//update largest
				largest = arr[index];
			}
			index++;
		}
		
		System.out.println("The largest number is : "+ largest);
	}
}

Output

Run the above Java program in your IDE or using Java command in command prompt.

The largest number is : 97

The program found out the largest integer in given integer array, as shown in the output.

Example 2 – Find Largest Number of Array using For Loop

In our previous example, we have taken an integer array. So, In this example, we shall take a float array and find largest floating point number using Java For Loop.

Solution

  1. Take a floating point array with some elements.
  2. Initialize a variable largest with the lowest of the Float value, Float.MIN_VALUE. This ensures that the largest picks the first element of the given array, in first iteration of the loop.
  3. Take a variable index. We shall use it to access elements of the array. Initialize index with zero in For Loop initialization part. We shall start from the left of array.
  4. Write a for loop that executes when index is less than length of the integer array. Increment index during each iteration.
    1. Inside for loop, check if largest is less than this element. If so, then update the largest with this element. If largest is not less than this element, we are happy with existing value of largest, nothing to do, so no else block required.
  5. After the for loop execution is done, we end with the largest element of array, in the variable largest.

Java Program

</>
Copy
/**
 * Java Program - Find Largest Number of an Array
 */

public class LargestNumberArray {
	public static void main(String[] args) {
		//an array
		float[] arr = {2.5f, 6.9f, 4.1f, 9.7f, 2.2f, 3.4f};
		//initialize with smallest possible value
		float largest = Float.MIN_VALUE;
		
		//find largest element of array
		for(int index = 0; index < arr.length; index++) {
			//check if largest is smaller than element
			if( largest < arr[index] ) {
				//update largest
				largest = arr[index];
			}
		}
		System.out.println("The largest number is : "+ largest);
	}
}

Output

Run the above Java Program in your IDE or command prompt using Java command.

The largest number is : 9.7

The program found the largest floating point number in given floating point array as shown in the output.

Example 3 – Find Largest Number of Array using Advanced For Loop

In this example, we shall take a double array and find largest number using Java Advanced For Loop.

Solution

  1. Take a double array with some elements.
  2. Initialize a variable largest with the lowest of the Double value, Double.MIN_VALUE. This ensures that the largest picks the first element of the given array, in first iteration of the loop.
  3. Write an advanced for loop that iterates over each element of the double array.
    1. Inside for loop, check if largest is less than this element. If so, then update the largest with this element. If largest is not less than this element, we are happy with existing value of largest, nothing to do, so no else block required.
  4. After the advanced for loop execution is done, we end with the largest element of array, in the variable largest.

Java Program

</>
Copy
/**
 * Java Program - Find Largest Number of an Array
 */

public class LargestNumberArray {
	public static void main(String[] args) {
		//an array
		double[] arr = {2.5, 6.9, 4.1, 9.7, 2.2, 3.4};	
		//initialize with smallest possible value
		double largest = Integer.MIN_VALUE;
		
		//find largest element of array
		for(double element : arr) {
			//check if largest is smaller than element
			if( largest < element ) {
				//update largest
				largest = element;
			}
		}
		System.out.println("The largest number is : "+ largest);
	}
}

Output

Run the above Java Program in your IDE or command prompt using Java command.

The largest number is : 9.7

The program found the largest double in given double array as shown in the output.

Conclusion

In this Java Tutorial, we learned how to find the largest number of a given array, using different looping statements in Java.