# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
Find Smallest Number in Array Java | Array Programs in Java – 13 | In the previous Java program, we developed programs to find the largest number in a given array using linear search. Now, let us discuss how to find the smallest number in array Java. Or, how to find min value in array Java?
Program description:- Write a Java program to find the smallest number in Java.
Procedure to develop the method to find the smallest number in array Java,
a) Take a one-dimensional array (assume array variable as arr)
b) Declare a variable min
c) Assign the first element of the array to the smallest variable i.e. min = arr[0]
d) Iterate through all elements of the array using the loop
e) Check the ith element in the array is lesser than min?
f) If yes, then update min variable i.e. assign the ith element to the min
g) Else, go to the next step
h) Repeat (e) to (g) until the end of the array.
i) Return min
Java Method to Find Smallest Number in Array Java
// Java method to find smallest number in array
public static int smallest(int[] arr) {
// declare a variable min
int min = 0;
// assign first element to min
min = arr[0];
// compare with remaining elements
// loop
for (int i = 1; i < arr.length; i++) {
// compare
if (min > arr[i])
min = arr[i];
}
// return result
return min;
}
At the start, we assume that the first element of the array is the smallest number and then compare it with the remaining elements. If any element is lesser than it, then assume that number as the smallest number. At last, we will get the smallest number in the given array.
At starting we should not continue with min = 0 because the array may have positive numbers and since 0 is always lesser than positive numbers, therefore, we may get the result as the smallest 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 smallest number in the given array we will display the result.
Program to Find Smallest Number in Array Java
import java.util.Scanner;
public class ArrayProgram {
// Java method to find smallest number in array
public static int smallest(int[] arr) {
int min = 0;
min = arr[0];
for (int i = 1; i < arr.length; i++) {
if (min > arr[i])
min = arr[i];
}
return min;
}
// main method
public static void main(String[] args) {
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// declare variables
int length = 0;
int numbers[] = null;
// take length of the array
System.out.print("Enter length of the array: ");
length = scan.nextInt();
// create array
numbers = new int[length];
// take array inputs
System.out.println("Enter array elements: ");
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scan.nextInt();
}
// method call
System.out.println("Smallest element = "
+ smallest(numbers));
// close Scanner
scan.close();
}
}
Output for the different test-cases:-
Enter length of the array: 5
Enter array elements:
25 40 10 17 45
Smallest element = 10
Enter length of the array: 5
Enter array elements:
10 -20 0 -50 90
Smallest element = -50
In this Java program, first, we created a Scanner class object to get input values from the end-user. Then, the length of the array and array elements are asked from the end-user and stored into appropriate variables. On this array, we called the smallest() method.
The smallest() method is given to find the smallest number in the array Java. Based on the previous code it finds and returns the smallest element among the array.
Find the Smallest Number in the Array using Stream
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] numbers = { 10, 5, -15, 20, -30 };
int smallest = Arrays.stream(numbers).min().getAsInt();
System.out.println(smallest); // -30
}
}
In the case of a list, we can do it as follows:-
import java.util.List;
public class Test {
public static void main(String[] args) {
List<Integer> numbers = List.of(10, 5, -15, 20, -30);
Integer smallest = numbers.stream()
.min((n1, n2) -> Integer.compare(n1, n2))
.get();
System.out.println(smallest); // -30
}
}
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!