How to find largest and smallest number from integer array - Java Solution (original) (raw)
Hello guys, if you have gone through any coding interview or have done some professional Software development then you know that a good understanding of array data structure is crucial for any software developer but it doesn't come for free, you need to spend time and effort. The best way to develop this understanding by solving coding problems and there are lots of programming exercises beginners can do. One of them is writing a program to find the smallest and largest number in an integer array. Java programmers are no different than others, so they can do this program in Java, not just to understand array but also different relational operators available in Java.
In this program, you need to write a method, yes we call the function a method in Java, which will accept an integer array and then print the largest and smallest number from that array. Use of any third-party library or API method is not allowed, which means you need to do this exercise by using essential tools of Java programming language, which includes operators, control statements, keywords, and some classes from java.lang package.
This problem is also known as finding maximum and minimum numbers in an array, and the technique mentioned here can be used in any other programming language as well. As a bonus point, you can also write JUnit test cases to test your method, I have not done so and relied on a simple main method to test my code to show the output and keep it short, essential for any example or demo.
Btw, if you preparing for a programming job interview, then let me repeat that a good knowledge of essential data structures like an array, linked list, binary tree, the hash table goes a long way in doing well on the interview. You should spend some time learning those and filling gaps in your understanding.
Java Program to find the smallest and largest number in an integer array
Here is a full code example of a Java program to find the smallest and largest number from an integer array. You can create a Java source file with the name MaximumMinimumArrayDemo.java and copy code there to compile and execute it in your favorite IDE.
If you don't have an IDE setup, you can also compile and run this program by following the steps I have shown on HelloWorld in Java.
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 the 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 are 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 the largest, it can't be smaller than the 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 the 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.
Btw, if you preparing for a programming job interview, then don't forget to check the Cracking the Coding Interview book. It contains 189 Programming Questions and Solutions, which is more than enough for many coding interviews.
Java Program to find the largest and smallest element in an array:
Here is the Java program I am talking about. This shows you how to find the maximum and minimum number in a given array in Java, without using any library method.
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.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
That's all about How to find the largest and smallest number from integer array in Java. As I said, this question can also be asked to find the maximum and minimum numbers in an Array in Java, so don't get confused there. By the way, there are more ways to do the same task, and you can practice it to code solutions differently. Can you write a solution that is different than this? Go ahead and give it a try.
Some more programs for coding interviews:
- How to check if a given number is prime or not? (solution)
- 10 Free Courses to learn Data Structure and Algorithms (courses)
- How to find the highest occurring word from a text file in Java? (solution)
- 100+ Data Structure and Algorithms Problems (solved)
- 10 Books to learn Data Structure and Algorithms (books)
- How to find if given String is a palindrome in Java? (solution)
- How to reverse an int variable in Java? (solution)
- How do you swap two integers without using the temporary variable? (solution)
- Write a program to check if a number is a power of two or not? (solution)
- How to reverse String in Java without using StringBuffer? (solution)
- How do you reverse the word of a sentence in Java? (solution)
- 10 Data Structure and Algorithms course to crack coding interview (courses)
- How to find a missing number in a sorted array? (solution)
- How to find the square root of a number in Java? (solution)
- How to calculate the GCD of two numbers in Java? (solution)
- How to find duplicate characters from a given String? (solution)
- Top 10 Programming problems from Java Interviews? (article)
P. S. - If you are preparing for a programming job interview, then you must prepare for an all-important topic like data structure, String, array, etc. One book which can help you with this task is the Grokking the Coding Interview: Patterns for Coding Questions course on . It contains popular coding interview patterns which will help you to solve most of the problems in your coding interviews.