In this article, we will discuss how to find smallest number in an Arrays and List
1. Finding Smallest number in an Arrays
We will follow below 3 approaches to get Smallest number in an Arrays
- Using SortedSet and TreeSet
- Using Arrays sorting
- Using Iterative approach
1.1 Using SortedSet & TreeSet approach
- Create TreeSet object with SortedSet reference and pass anonymous Comparator object with logic for descending-order sorting of integer numbers
- And add actual Arrays (as List after converting) to newly created TreeSet object which will sort integer numbers in descending order
- Now, elements inside TreeSet object will be sorted according to descending-order and last element will be the smallest element
- Using last() method of SortedSet reference, we can get last element and that will be the smallest element
FindSmallestNumberInAnArraysUsingTreeSet.java
package in.bench.resources.smallest.number;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;
import java.util.SortedSet;
import java.util.TreeSet;
public class FindSmallestNumberInAnArraysUsingTreeSet {
public static void main(String[] args) {
// random numbers
Integer[] numbers = {5, 9, 11, 2, 8, 21, 1};
// print to console
System.out.println("Original Integer Arrays - "
+ Arrays.toString(numbers));
// Execution - start time
LocalTime startTime = LocalTime.now();
// sort Integer[] arrays using TreeSet - stores in DESC order
SortedSet<Integer> sortedSet = new TreeSet<Integer>(
new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return Integer.compare(i2, i1);
}
}
);
// add int[] arrays elements to TreeSet
sortedSet.addAll(Arrays.asList(numbers));
// get smallest number in an Arrays
int smallestNumber = sortedSet.last();
// Execution - end time
LocalTime endTime = LocalTime.now();
// find difference
Duration duration = Duration.between(startTime, endTime);
long differenceInNano = duration.getNano();
// print sum to console
System.out.println("\nSmallest number in an Arrays is - "
+ smallestNumber);
// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Original Integer Arrays - [5, 9, 11, 2, 8, 21, 1]
Smallest number in an Arrays is - 1
Execution time - 0 ns
1.2 Using Arrays sorting approach
- First step is to sort Arrays of integer numbers using Arrays.sort() method in descending order
- Arrays.sort() method accepts 2 input-arguments
- 1st argument is the actual Arrays to be sorted
- 2nd argument is the anonymous Comparator object with logic for descending-order sorting of integer numbers
- After sorting, get smallest number by passing index of last element to integer Arrays
FindSmallestNumberInAnArraysUsingSortMethod.java
package in.bench.resources.smallest.number;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;
public class FindSmallestNumberInAnArraysUsingSortMethod {
public static void main(String[] args) {
// random numbers
Integer[] numbers = {5, 9, 11, 2, 8, 21, 1};
// print to console
System.out.println("Original Integer Arrays - "
+ Arrays.toString(numbers));
// Execution - start time
LocalTime startTime = LocalTime.now();
// sort Arrays elements in descending order
Arrays.sort(numbers, new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return Integer.compare(i2, i1);
}
});
// last element will be smallest number in an Arrays
int smallestNumber = numbers[numbers.length - 1];
// Execution - end time
LocalTime endTime = LocalTime.now();
// find difference
Duration duration = Duration.between(startTime, endTime);
long differenceInNano = duration.getNano();
// print sum to console
System.out.println("\nSmallest number in an Arrays is - "
+ smallestNumber);
// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Original Integer Arrays - [5, 9, 11, 2, 8, 21, 1]
Smallest number in an Arrays is - 1
Execution time - 0 ns
1.3 Using Iterative approach
- First assume 1st element of an Arrays is the smallest number
- Then start iterating Arrays one-by-one using for-loop and compare assumed smallest number with other iterating numbers and accordingly set smallest number
FindSmallestNumberInAnArraysUsingIterativeApproach.java
package in.bench.resources.smallest.number;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
public class FindSmallestNumberInAnArraysUsingIterativeApproach {
public static void main(String[] args) {
// random numbers
Integer[] numbers = {5, 9, 11, 2, 8, 21, 1};
// print to console
System.out.println("Original Integer Arrays - "
+ Arrays.toString(numbers));
// Execution - start time
LocalTime startTime = LocalTime.now();
// assume smallest number
int min = numbers[0];
// iterate and find smallest number
for(int index = 0; index < numbers.length; index++) {
if(numbers[index] < min) {
min = numbers[index];
}
}
// Execution - end time
LocalTime endTime = LocalTime.now();
// find difference
Duration duration = Duration.between(startTime, endTime);
long differenceInNano = duration.getNano();
// print sum to console
System.out.println("\nSmallest number in an Arrays is - "
+ min);
// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Original Integer Arrays - [5, 9, 11, 2, 8, 21, 1]
Smallest number in an Arrays is - 1
Execution time - 0 ns
2. Finding Smallest number in List
We will follow below 2 approaches to get smallest number in a List or ArrayList
- Using Collection sorting
- Using Iterative approach
2.1 Using Collection sorting approach
- First step is to sort List of integer numbers using Collections.sort() method in descending order
- Collections.sort() method accepts 2 input-arguments
- 1st argument is the actual List to be sorted
- 2nd argument is the anonymous Comparator object with logic for descending-order sorting of integer numbers
- After sorting, get smallest number by passing index of last element to integer List
FindSmallestNumberInListUsingCollectionsSortMethod.java
package in.bench.resources.smallest.number;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class FindSmallestNumberInListUsingCollectionsSortMethod {
public static void main(String[] args) {
// random numbers
List<Integer> numbers = Arrays.asList(5, 9, 11, 2, 8, 21, 1);
// print to console
System.out.println("Original Integer List - "
+ numbers);
// Execution - start time
LocalTime startTime = LocalTime.now();
// sort List element in descending order
Collections.sort(numbers, new Comparator<Integer>() {
@Override
public int compare(Integer i1, Integer i2) {
return Integer.compare(i2, i1);
}
});
// last element will be smallest number in an Arrays
int smallestNumber = numbers.get(numbers.size() - 1);
// Execution - end time
LocalTime endTime = LocalTime.now();
// find difference
Duration duration = Duration.between(startTime, endTime);
long differenceInNano = duration.getNano();
// print sum to console
System.out.println("\nSmallest number in List is - "
+ smallestNumber);
// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Original Integer List - [5, 9, 11, 2, 8, 21, 1]
Smallest number in List is - 1
Execution time - 0 ns
2.2 Using Iterative approach
- First assume 1st element of List is the smallest number
- Then start iterating List one-by-one using for-loop and compare assumed smallest number with other iterating numbers and accordingly set smallest number
FindSmallestNumberInListUsingIterativeApproach.java
package in.bench.resources.smallest.number;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
public class FindSmallestNumberInListUsingIterativeApproach {
public static void main(String[] args) {
// random numbers
List<Integer> numbers = Arrays.asList(5, 9, 11, 2, 8, 21, 1);
// print to console
System.out.println("Original Integer List - "
+ numbers);
// Execution - start time
LocalTime startTime = LocalTime.now();
// assume smallest number
int min = numbers.get(0);
// iterate and find smallest number
for(int index = 0; index < numbers.size(); index++) {
if(numbers.get(index) < min) {
min = numbers.get(index);
}
}
// Execution - end time
LocalTime endTime = LocalTime.now();
// find difference
Duration duration = Duration.between(startTime, endTime);
long differenceInNano = duration.getNano();
// print sum to console
System.out.println("\nSmallest number in List is - "
+ min);
// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Original Integer List - [5, 9, 11, 2, 8, 21, 1]
Smallest number in List is - 1
Execution time - 0 ns
3. Points to remember w.r.t execution time:
- Execution time differs in different platforms
- With small set of numbers, we may not find large difference in execution time
- But with large set of numbers, difference will be significant to consider
Related Articles:
- Java 8 – Find Largest number in an Arrays or List or Stream ?
- Java 8 – Find Smallest number in an Arrays or List or Stream ?
- Java 8 – Find 2nd Largest number in an Arrays or List or Stream ?
- Java 8 – Find 2nd Smallest number in an Arrays or List or Stream ?
- Java 8 – Find sum of Largest 2 numbers in an Arrays or List or Stream ?
- Java 8 – Find sum of Smallest 2 numbers in an Arrays or List or Stream ?
- Java 8 – Find 1st and Last elements in an Arrays ?
- Java 8 – Find 1st and Last elements in a List or ArrayList ?
- Java 8 – Find 1st and Last elements in a Set or HashSet ?
- Java 8 – Find 1st and Last entries in a Map or HashMap ?
- Java 8 – Find sum and average of a List or ArrayList ?
- Java 8 – How to calculate sum and average of an Arrays ?
Happy Coding !!
Happy Learning !!