Java – Find Second Smallest number in an Arrays or List ?

In this article, we will discuss how to find second smallest number in an Arrays and List

1. Finding Second smallest number in an Arrays

We will follow below 2 approaches to get 2nd Smallest 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 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
• Remove last element using remove() method of SortedSet reference and now last element in the TreeSet object will be the 2nd smallest number

FindSecondSmallestNumberInAnArraysUsingTreeSet.java

```package in.bench.resources.second.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 FindSecondSmallestNumberInAnArraysUsingTreeSet {

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 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

// remove last element which will be largest number in an Arrays
sortedSet.remove(sortedSet.last());

int secondSmallestNumber = 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 smallest number in an Arrays is - "
+ secondSmallestNumber);

// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
```

Output:

```Second smallest number in an Arrays is - 2

Execution time - 2000000 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 2nd smallest number by passing index of second-last element to integer Arrays

FindSecondSmallestNumberInAnArraysUsingSortMethod.java

```package in.bench.resources.second.smallest.number;

import java.time.Duration;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.Comparator;

public class FindSecondSmallestNumberInAnArraysUsingSortMethod {

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 elements in descending order
Arrays.sort(numbers, new Comparator<Integer>() {

@Override
public int compare(Integer i1, Integer i2) {
return Integer.compare(i2, i1);
}
});

// 2nd last element will be second smallest number in an Arrays
int secondSmallestNumber = 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 smallest number in an Arrays is - "
+ secondSmallestNumber);

// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
```

Output:

```Second smallest number in an Arrays is - 2

Execution time - 0 ns
```

2. Finding second smallest number in List

• 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 2nd smallest number by passing index of second-last element to integer List

FindSecondSmallestNumberInListUsingCollectionsSortMethod.java

```package in.bench.resources.second.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 FindSecondSmallestNumberInListUsingCollectionsSortMethod {

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 descending order
Collections.sort(numbers, new Comparator<Integer>() {

@Override
public int compare(Integer i1, Integer i2) {
return Integer.compare(i2, i1);
}
});

// 2nd last element will be second smallest number in an Arrays
int secondSmallestNumber = 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 smallest number in List is - "
+ secondSmallestNumber);

// print execution time in Nano seconds
System.out.println("\nExecution time - "
+ differenceInNano + " ns");
}
}
```

Output:

```Second smallest number in List is - 2

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:

Happy Coding !!
Happy Learning !!