Find Largest Number in Array Java

Find Largest Number in Array Java | Array Programs in Java – 12 | In the previous Java program, we have developed programs to merge two sorted arrays in Java. Now in this program, we will find the largest number in array Java? How to find max value in array Java?

Program description:- Write a Java program to find the largest number in the array.

Example:-

Array: {10, 5, -15, 20, -30}
Largest element = 20

Procedure to develop the method to find the largest number in Array Java,

a) Take a one-dimensional array (assume array variable as arr)
b) Declare a variable max
c) Assign first element of the array to largest variable i.e. max = arr[0]
d) Iterate through all elements of the array using the loop
e) Check the ith element in the array is greater than max?
f) If yes, then update max variable i.e. assign the ith element to the max
g) Else, go to the step
h) Repeat (e) to (g) until the end of the array.
i) Return max

Java Method to Find the Largest Number in an Array

// Java method to find largest number in array
public static int largest(int[] array) {

  // declare a variable max
  int max = 0;

  // assign first element to max
  max = array[0];

  // compare with remaining elements
  // loop
  for (int i = 1; i < array.length; i++) {
    // compare 
    if (max < array[i]) max = array[i];
  }

  return max;
}

At the start, we assume that the first element of the array is the largest number and then compare it with the remaining elements. If any element is larger than it, then assume that number as the largest number. At last, we will get the largest number in the given array.

At starting we should not continue with max = 0 because the array may have negative numbers and since 0 is always greater than negative numbers, therefore, we may get a result as the largest number = 0.

Using the above method let us develop the Java program. We will take array input from the end-user, first, we will ask to enter the size of the array and then enter the array elements. To take input from the end-user we will use the Scanner class, but you can also use the BufferedReader class. After finding the largest number in the given array we will display the result.

Java Program to Find the Largest or Max Number in Array

import java.util.Scanner;

public class ArrayProgram {

  // Java method to find largest number in array
  public static int largest(int[] array) {
    
    // declare a variable max
    // assign first element to max
    int max = array[0];

    // compare with remaining elements
    // loop
    for (int i = 1; i < array.length; i++) {
      if (max < array[i])
        max = array[i];
    }

    return max;
  }
  
  // main method
  public static void main(String[] args) {
    // create Scanner class object to read input
    Scanner scan = new Scanner(System.in);

    // declare variables
    int size = 0;
    int arr[] = null;

    // take length of the array
    System.out.print("Enter length of the array: ");
    size = scan.nextInt();

    // create array
    arr = new int[size];

    // take array inputs
    System.out.println("Enter array elements: ");
    for (int i = 0; i < arr.length; i++) {
      arr[i] = scan.nextInt();
    }

    // method call
    System.out.println("Largest element = " + largest(arr));

    // close Scanner
    scan.close();
  }
}

Output for the different test-cases:-

Enter length of the array: 5
Enter array elements:
10 5 -15 20 -30
Largest element = 20

Enter length of the array: 3
Enter array elements:
45 54 30
Largest element = 54

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *