import scala.util.Random
object SecondMaxFinder {
def findSecondMax(total: Int, rndmax: Int): Option[Int] = {
val rng = new Random()
val numbers = List.fill(total) {
val n = rng.between(1, rndmax + 1)
println(n)
n
}
val uniqueSorted = numbers.distinct.sorted(Ordering[Int].reverse)
uniqueSorted.drop(1).headOption
}
def main(args: Array[String]): Unit = {
val secondMax = findSecondMax(10, 100)
secondMax match {
case Some(value) => println(s"The second biggest number is: $value")
case None => println("Not enough unique numbers to determine a second maximum.")
}
}
}
/*
run:
6
30
95
34
84
100
1
1
18
19
The second biggest number is: 95
*/