How to find the longest and the shortest word in a list with Scala

1 Answer

0 votes
object Main extends App {
  val lst = List("scala", "c", "c++", "python", "java")
   
  var maxs = lst.maxBy(_.length)
  var mins = lst.minBy(_.length)
   
  println(s"Longest word: $maxs")
  println(s"Shortest word: $mins")
}
 
 
 
/*
run:
 
Longest word: python
Shortest word: c
 
*/


answered Oct 10, 2024 by avibootz
edited Oct 10, 2024 by avibootz
...