I ran into a problem where I had to find out the elements which were repeated the most in the list, sort them and drop the duplicates.
I was scanning through the lists module and was kinda surprised that no such function exist.
For example I have a list:
Here’s how it works in Smalltalk
aCollection asBag
“converts any collection to an {element → count} table”
sortedCounts
"converts to a sequence of element->count associations in
decreasing order of count"
collect: [:each | each key]
“strips off the counts”
This is in a language with more kinds of collections than you’ve dreamed
of and more operations on them than Erlang’s worst nightmares. The
combination you want is not there but the pieces you need to build it
trivially are.
So look for suitable pieces that might be useful to other people and
recommend those.
Same solution that I came up. Although I feel my code is kinda clunky
Lots of code for such simple operation(order elements by popularity).
I have checked some examples in other languages and the solutions seemed to be having same amount of code.
Probably not so common issue.
most(List) ->
M = lists:foldl(fun(E, A) -> maps:update_with(E, fun(V) -> V + 1 end, 1, A) end, #{}, List),
lists:sort(fun(A, B) -> maps:get(A, M) >= maps:get(B, M) end, maps:keys(M)).
OK there is a more general issue here.
Since the counts are positive integers bounded by the
length of the original list, it ought to be possible to
do the whole thing in linear time. But there is no
“sort by modest integer keys” function in the library.
BTW, I am having a hard time believing that it is useful
to answer [x,y] with no indication of whether x occured a
million times more often than y or the same number of times.