Finding the largest and the smallest numbers in an array in Java

Finding the Largest and the Smallest Numbers in an Array in Java: Video Lecture 17

Finding the largest and smallest numbers in an array is a common programming problem. This post describes how to write a Java program to find the largest or the smallest element in a one-dimensional array. 

In Java Video Lecture 16, we gave two exercise questions on one-dimensional arrays. The exercise questions are as follows.

  • Write a program that starts with an array of ten integers. Print all the numbers on the terminal. Then print the largest number in the array.
  • Write a program that starts with an array of ten integers. Print all the numbers on the terminal. Then print the smallest number in the array.

The Youtube video below explains solutions to the exercise questions mentioned above.

Finding the largest number in an array

Let us assume that we have an integer array, named a, that contains the integers 20, 22, 40, 18, 23, 25, 29, 35, 21, and 17. Let us assume that we will hard-code the numbers as an initialization.

Therefore, we will write the following line.

int[] a={20, 22, 40, 18, 23, 25, 29, 35, 21, 17};

Now, we will write the code to find the largest number in the array a.

Before we write the code to find the largest number, let us write a for loop to print all the numbers on the screen. We use a for loop, with a loop variable named i, to print all the numbers of the array. Here is how we will print all the numbers separated by spaces.

    int i;
    for (i=0; i<a.length; i=i+1){
      System.out.print(a[i]+" ");
    }
    System.out.println();

The line “System.out.println();” is to make sure that there is a new line after printing all the numbers.

Now we will write code to find and print the largest number. The logic behind finding the largest number is the following. 


  •  We will read one number at a time and see if the number we are currently looking at is greater than the largest number we have seen so far.
  • If the number we are looking at is larger than the largest number we have seen so far, then we will remember the number that we are looking at as the largest number.
  • Once we go over all the numbers in the array using this logic, we will find that we have remembered the largest element of the array.

To remember the largest number we have seen so far, we will use a variable. In our code, we will use a variable named largest

Initially, we can just look at the number in the beginning of the array, which is in index 0, and consider that the number in index 0 is the largest number we have seen so far. Therefore, we can declare the variable largest and initialize it to a[0] using the following syntax.

    int largest=a[0];

Then we can go over each of the rest of the indices and check if the content in a later index is larger than what we have in the variable named largest

We will write a loop that will take us to all the indices between 1 and the last index of the array. We need a variable to go over the indices. Let the variable be i. Note that we already copied the content of index zero in the variable largest. Therefore, we do not need to start the loop from zero; rather, we start the loop from 1. 

We will end the loop when the value of the loop variable i becomes equal to the length of the array. That means, as long as i is smaller than the length of the array, we go inside the loop. When i becomes equal to or greater than the length of the array, the loop terminates. In this case, our loop will terminate when i is equal to the length of the array.

Note from Java Video Lecture 16 that the last valid index of the array is one less than the length of the array, a.length. Therefore, the loop termination condition will be i<a.length

The change part of the loop is straight forward because we are going over every index. That means we will increment the loop variable i by one at the end of each iteration (that is, i=i+1). The following lines provide the loop and the logic required to find the largest number in the variable named largest.

    for (i=1; i<a.length; i=i+1){
      if (a[i]>largest){
        largest=a[i];
      }
    }

 When we are inside the loop, a[i] gives us the content in index i of the array. We check if the number in index i, which is a[i], is greater than the number stored in the variable largest using the following if statement: if (a[i]>largest).

Inside the scope of the if statement, a[i] is higher than the number that we remembered in the variable named largest. That means, we have to remember a[i] as our largest number seen so far. Therefore, we copy a[i] to the variable largest using the syntax largest=a[i];

Once the for loop goes over all the elements in the array, we will have the largest number in the variable named largest.

That is, after the for loop ends, the variable named largest contains the largest number of the entire array.

After the execution of the for loop, we can write the content of the variable largest. We use a System.out.println statement, which provides this nice text, “The largest number in the array is: ” then we write the content of the variable largest.

    System.out.println(
            "The largest number in the array is: "+largest);

The complete code, written in a file named LargestNum.java, is provided below.

class LargestNum{
  public static void main(String[] args){
    int[] a={20, 22, 40, 18, 23, 25, 29, 35, 21, 17};
    int i;
    for (i=0; i<a.length; i=i+1){
      System.out.print(a[i]+" ");
    }
    System.out.println();
    int largest=a[0];
    for (i=1; i<a.length; i=i+1){
      if (a[i]>largest){
        largest=a[i];
      }
    }
    System.out.println(
    "The largest number in the array is: "+largest);
  }
}

The output of the program is as follows.

Figure: The output of LargestNum.java.

Finding the smallest number in an array

Finding the smallest number uses the same logic as finding the largest number. Therefore, we will change the previous code instead of starting from scratch.

Printing all the numbers is exactly the same as the previous exercise.

Instead of using a variable named largest, we will use a variable named smallest to remember the smallest number we have seen so far. Here is the core code to find the smallest number.

    int smallest=a[0];
    for (i=1; i<a.length; i=i+1){
      if (a[i]<smallest){
        smallest = a[i];
      }
    }

The content in index zero will be kept in the variable named smallest first. 

In the if statement, previously, we checked if a number stored in index i is greater than the content in the variable largest. Now, we want to check if the content in index i is smaller than the number remembered in the variable smallest. Therefore, we write “if (a[i]<smallest)“.

If we find that the content in index i, which is a[i], is smaller than the smallest number we have remembered so far, we will assign a[i] to the variable named smallest.

 Of course, finally, we will print the variable “smallest”, using the following statement.

    System.out.println(
     "The smallest number in the array is: "+smallest);

Here is the complete code written in a file named SmallestNum.java.

class SmallestNum{
  public static void main(String[] args){
    int[] a={20, 22, 40, 18, 23, 25, 29, 35, 21, 17};
    int i;
    for (i=0;i<a.length;i=i+1){
      System.out.print(a[i]+" ");
    }
    System.out.println();
    int smallest=a[0];
    for (i=1; i<a.length; i=i+1){
      if (a[i]<smallest){
        smallest = a[i];
      }
    }
    System.out.println(
     "The smallest number in the array is: "+smallest);
  }
}

The output of the program is provided below. Notice that the smallest number is printed on the terminal.

Figure: The output of SmallestNum.java.

Additional exercise

  • Find the largest even number in an array. For example, if you have the following array {5, 10, 9, 8, 6, 20, 11, 23}, the program should print 20 because it is the largest even number in the array.
  • Print all the odd numbers in an array. For example, if you have the following array {5, 10, 9, 8, 6, 20, 11, 23}, the program should print 5 9 11 23 because these are the odd numbers in the array.
  • Write a program that will print each other numbers of an array. For example, if an array contains {5, 10, 9, 8, 6, 20, 11}, then the output should be 5 9 6 11.

Concluding remarks

An array is a widely used data structure in any programming language. Practicing with arrays is essential for beginners. Please make sure to practice to improve fluency in the use of arrays in Java. So far, we used integer arrays (int[]), You can practice using other number arrays like double[], float[], long[], and short[].

If you haven’t subscribed to our website Computing4All.com and to this YouTube channel where we host our video lectures, please do so to receive notifications on our new articles and videos.

Please do not hesitate to contact us via the comments section below. 

Happy learning!

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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