Scala, find largest list in a list

Viewed 98

Lets say val a = List[List[int]]: What is the cleanest way to find the list that contains the most amount of elements in Scala?

2 Answers

Try one of .maxBy or .maxByOption

List(List(0)).maxBy(_.size) // List(0)
List(List(0)).maxByOption(_.size) // Some(List(0))
List.empty[List[Int]].maxByOption(_.size) // None
List(List(1), List(2, 3)).maxBy(_.size)
Related