I have a list of words (with repetitions), and I intend to find out the longest words amongst them, along with their length. My question is to know if my implementation is too verbose, is using too many operators/functions and if so, what are the better, more idiomatic, readable alternatives.
Given the list below, the expected output is
Max length=3, words are "mnp","pqr","jkl","abc","xyz"
val line = List("a","ab","abc","xyz","mnp","ac","d","b","ab","bc","bd","be","bf","b","abc","abc","pqr","mnp","jkl","a","b")
val lengthAndTheWords = line.map(x => (x.length,x)).groupBy(x => x._1).mapValues(x => x.toSet)
val longest = lengthAndTheWords.toSeq.sortBy(key => -key._1).head
println("Max length =" +
longest._1 +
", words are" +
longest._2.map(nextEntry => nextEntry._2).mkString(","))
(With an Ideone running the code)
I consider myself an intermediate level Scala programmer; so, I am looking for all tips to improve my skills in Scala.
xyz
? What'spqr
? etc. As it currently stands, this code is a bit invalid. \$\endgroup\$