最长单词(scala)

题目是

{
  "dog",
  "google",
  "facebook",
  "internationalization",
  "blabla"
}
中, 最长的单词集合为 ["internationalization"]
在词典
{
  "like",
  "love",
  "hate",
  "yes"
}
中,最长的单词集合为 ["like", "love", "hate"]

scala略入门的写法

val sort_array = List("dog",
    "google",
    "facebook",
    "internationalization",
    "blabla").sortWith(_.length<_.length)
sort_array.filter(_.length == sort_array.last.length).foreach(println)
List("dog",
    "google",
    "facebook",
    "internationalization",
    "blabla").map(str=>(str,str.length)).maxBy(_._2)._1

你可能感兴趣的:(最长单词(scala))