user3799968
user3799968

Reputation: 1155

Scala, find largest list in a list

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?

Upvotes: 2

Views: 145

Answers (2)

Ivan Stanislavciuc
Ivan Stanislavciuc

Reputation: 7275

List(List(1), List(2, 3)).maxBy(_.size)

Upvotes: 1

Mateusz Kubuszok
Mateusz Kubuszok

Reputation: 27535

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

Upvotes: 5

Related Questions