In this article, we will discuss how to find second largest number in an Arrays and List
1. Finding second largest number in an Arrays
We will follow below 2 approaches to get 2nd Largest number in an Arrays
- Using SortedSet and TreeSet
- Using Arrays sorting
1.1 Using SortedSet & TreeSet approach
- Create TreeSet object with SortedSet reference and pass actual Arrays (as List after converting) as constructor-argument to be sorted
- Now, elements inside TreeSet object will be sorted according to natural-order and last element will be the largest element
- Using last() method of SortedSet reference, we can get last element and that will be the largest element
- Remove last element using remove() method of SortedSet reference and now the last element in the TreeSet object will be the 2nd largest number
FindSecondLargestNumberInAnArraysUsingTreeSet.java
package in.bench.resources.second.largest.number;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.SortedSet;
import java.util.TreeSet;
public class FindSecondLargestNumberInAnArraysUsingTreeSet {
public static void main(String[] args) {
// random numbers
Integer[] numbers = {5, 9, 11, 2, 8, 21, 1};
// Execution - start time
LocalTime startTime = LocalTime.now();
// sort Integer[] arrays using TreeSet - stores in ASC order
SortedSet<Integer> sortedSet = new TreeSet<Integer>(
Arrays.asList(numbers) // convert arrays to List
);
// remove last element which will be largest number in an Arrays
sortedSet.remove(sortedSet.last());
// now, this will be second largest number in an Arrays
int secondLargestNumber = 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("Second largest number in an Arrays is - "
+ secondLargestNumber);
// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Second largest number in an Arrays is - 11
Execution time - 2000000 ns
1.2 Using Arrays sorting approach
- First step is to sort Arrays of integer numbers using Arrays.sort() method in ascending order
- After sorting, get 2nd largest number by passing index of second-last element to integer Arrays
FindSecondLargestNumberInAnArraysUsingSortMethod.java
package in.bench.resources.second.largest.number;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
public class FindSecondLargestNumberInAnArraysUsingSortMethod {
public static void main(String[] args) {
// random numbers
Integer[] numbers = {5, 9, 11, 2, 8, 21, 1};
// Execution - start time
LocalTime startTime = LocalTime.now();
// sort Arrays element in ascending order
Arrays.sort(numbers);
// 2nd last element will be second largest number in an Arrays
int secondLargestNumber = numbers[numbers.length - 2];
// 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("Second largest number in an Arrays is - "
+ secondLargestNumber);
// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Second largest number in an Arrays is - 11
Execution time - 1000000 ns
2. Finding second largest number in List
- First step is to sort List of integer numbers using Collections.sort() method in ascending order
- After sorting, get 2nd largest number by passing index of second-last element to integer List
FindSecondLargestNumberInListUsingCollectionsSortMethod.java
package in.bench.resources.second.largest.number;
import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class FindSecondLargestNumberInListUsingCollectionsSortMethod {
public static void main(String[] args) {
// random numbers
List<Integer> numbers = Arrays.asList(5, 9, 11, 2, 8, 21, 1);
// Execution - start time
LocalTime startTime = LocalTime.now();
// sort List element in ascending order
Collections.sort(numbers);
// 2nd last element will be second largest number in an Arrays
int secondLargestNumber = numbers.get(numbers.size() - 2);
// 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("Second largest number in List is - "
+ secondLargestNumber);
// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
Output:
Second largest number in List is - 11
Execution time - 1000000 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 !!