I used a map to scan an input file and produce a word count.
the map keys are words and the values are occurrences of the word.
I converted the map to a list and sorted ascending by value.
Thus, I have a list of 2 tuples, with the least frequently occurring word in the first position of the list, and the most frequently occurring word in the last position of the list.
I know I can easily get the 2tuple in the last position of the list with myList.last and then accessing the second element of the result with the myTuple._2
Can it be done in a single line of code?
I want to make it as concise as I can.
Do you mean:
This will get the last element of
muListand then the second item in the tuple found there.But you don’t need to convert to a
Listand sort and all that. You can just get the max-count element from theMap, and it will be much faster:Or, if you don’t care about the word at all: