So I have a map from which I’m trying to find the maximum occurring element for a key.
This is what I’m trying to do:-
Input
iex(1)> items = [%{name: 'soap', price: 50, quantity: 3}, %{name: 'pen', price: 10, quantity: 2}, %{name: 'pen', price: 14, quantity: 6}]
Output
[
%{name: 'soap', price: 50, quantity: 3},
%{name: 'pen', price: 10, quantity: 2},
%{name: 'pen', price: 14, quantity: 6}
]
Now I’ve grouped together similar element using group_by function
Input
iex(2)> items |> Enum.group_by(fn %{name: name } -> {name} end, fn %{quantity: quantity } -> {quantity} end )
Output
%{{'pen'} => [{2}, {6}], {'soap'} => [{3}]}
Now what I’m trying to find is
- how many items occur for “pen” and “soap” or what if it gives me maximum value occurs in a particular key?