Kotlin Program to Find Second Largest Number in List
list.sortedDescending()[1] or by iterating to track the largest and second largest values.Examples
How to Think About It
Algorithm
Code
fun findSecondLargest(numbers: List<Int>): String {
if (numbers.distinct().size < 2) return "No second largest element"
var largest = Int.MIN_VALUE
var secondLargest = Int.MIN_VALUE
for (num in numbers) {
if (num > largest) {
secondLargest = largest
largest = num
} else if (num > secondLargest && num < largest) {
secondLargest = num
}
}
return secondLargest.toString()
}
fun main() {
val list = listOf(3, 5, 1, 2, 4)
println(findSecondLargest(list))
}Dry Run
Let's trace the list [3, 5, 1, 2, 4] through the code to find the second largest number.
Initialize variables
largest = Int.MIN_VALUE, secondLargest = Int.MIN_VALUE
Check first number 3
3 > largest (-2147483648), so largest = 3, secondLargest = -2147483648
Check second number 5
5 > largest (3), so secondLargest = 3, largest = 5
Check third number 1
1 > secondLargest (3)? No, no change
Check fourth number 2
2 > secondLargest (3)? No, no change
Check fifth number 4
4 > secondLargest (3) and 4 < largest (5), so secondLargest = 4
Return result
secondLargest = 4
| Number | Largest | Second Largest |
|---|---|---|
| 3 | 3 | -2147483648 |
| 5 | 5 | 3 |
| 1 | 5 | 3 |
| 2 | 5 | 3 |
| 4 | 5 | 4 |
Why This Works
Step 1: Check list size
We first check if the list has at least two unique numbers using distinct() to avoid errors.
Step 2: Track largest and second largest
We keep two variables to remember the largest and second largest numbers as we scan the list once.
Step 3: Update values carefully
When a number is bigger than the largest, we move the largest to second largest and update largest. If it's between largest and second largest, we update second largest.
Alternative Approaches
fun findSecondLargest(numbers: List<Int>): String {
val distinctSorted = numbers.distinct().sortedDescending()
return if (distinctSorted.size < 2) "No second largest element" else distinctSorted[1].toString()
}
fun main() {
println(findSecondLargest(listOf(3, 5, 1, 2, 4)))
}fun findSecondLargest(numbers: List<Int>): String {
val max = numbers.maxOrNull() ?: return "No second largest element"
val filtered = numbers.filter { it != max }
val secondMax = filtered.maxOrNull() ?: return "No second largest element"
return secondMax.toString()
}
fun main() {
println(findSecondLargest(listOf(3, 5, 1, 2, 4)))
}Complexity: O(n) time, O(1) space
Time Complexity
The main approach scans the list once, so it runs in linear time O(n), where n is the number of elements.
Space Complexity
It uses only a few variables to track values, so space complexity is constant O(1).
Which Approach is Fastest?
The single scan method is fastest for large lists compared to sorting (O(n log n)) or filtering multiple times.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Single scan tracking | O(n) | O(1) | Large lists, best performance |
| Sorting and picking | O(n log n) | O(n) | Simple code, small lists |
| Max and filter | O(n) | O(n) | Readable but less efficient |