I have a word-frequency array like this:
[("hello", 1), ("world", 5), ("globle", 1)]
I have to reverse
it such that I get frequency-to-wordCount map like this:
[(1, 2), (5, 1)]
Notice that since two words ("hello" and "globe") have the frequency 1, the value of the reversed
mapping is 2
. However, since there is only one word with a frequency 5
, so, the value of that entry is 1
. How can I do this in scala?
Update:
I happened to figure this out as well:
arr.groupBy(_._2).map(x => (x._1,x._2.toList.length))