top of page
Writer's pictureThe Tech Platform

How to find largest and smallest number from integer array


If you look at the code here, we have created a method called the largestAndSmallest(int[] numbers) to print the largest and smallest number from int array passed to the program. We have used two variables largest and smallest, to store the maximum and minimum values from the array. Initially, the largest is initialized with Integer.MIN_VALUE and smallest is initialized with Integer.MAX_VALUE.


In each iteration of the loop, we compare the current number with the largest and smallest and update them accordingly. Since if a number is larger than largest, it can't be smaller than smallest, which means you don't need to check if the first condition is true, that's why we have used if-else code block, where else part will only execute if the first condition is not true.


Here is another logical diagram or flow chart to find the largest element from an array in Java, here instead of assigning the variable with Integer.MAX_VALUE, we have assigned the first element from the array.


Since array doesn't override the toString method in Java, we have used Arrays.toString() to print the contents of an array. Remember, this function is outside of core logic, so it's ok to use it. Since this is a static method, we can directly call this from the main method in Java, and so does our test code.


We pass the random array to this method and see if the largest and smallest number returned by the method is correct or not. For automated testing, a Unit test is better, but for a demonstration, you can use the main method.



Code:

import java.util.Arrays;
/**
 * Java program to find largest and smallest number from an array in Java.
 * You cannot use any library method both from Java and third-party library.
 *
 * @author http://java67.blogspot.com
 */public class MaximumMinimumArrayDemo{

    public static void main(String args[]) {
        largestAndSmallest(new int[]{-20, 34, 21, -87, 92,
                             Integer.MAX_VALUE});
        largestAndSmallest(new int[]{10, Integer.MIN_VALUE, -2});
        largestAndSmallest(new int[]{Integer.MAX_VALUE, 40,
                             Integer.MAX_VALUE});
        largestAndSmallest(new int[]{1, -1, 0});
    }

    public static void largestAndSmallest(int[] numbers) {
        int largest = Integer.MIN_VALUE;
        int smallest = Integer.MAX_VALUE;
        for (int number : numbers) {
            if (number > largest) {
                largest = number;
            } else if (number < smallest) {
                smallest = number;
            }
        }

        System.out.println("Given integer array : " + Arrays.toString(numbers));
        System.out.println("Largest number in array is : " + largest);
        System.out.println("Smallest number in array is : " + smallest);
    }
}

Output:

Given integer array : [-20, 34, 21, -87, 92, 2147483647]
Largest number in array is : 2147483647
Smallest number in array is : -87
Given integer array : [10, -2147483648, -2]
Largest number in array is : 10
Smallest number in array is : -2147483648
Given integer array : [2147483647, 40, 2147483647]
Largest number in array is : 2147483647
Smallest number in array is : 40
Given integer array : [1, -1, 0]
Largest number in array is : 1
Smallest number in array is : -1


Source: Java67


The Tech Platform

0 comments

Comments


bottom of page