Finding maximum occurring element in a map?

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

  1. how many items occur for “pen” and “soap” or what if it gives me maximum value occurs in a particular key?

:wave:

items
|> Enum.group_by(
  fn %{name: n} -> n end,
  fn %{quantity: q} -> q end
)
|> Enum.map(fn {k, v} -> {k, Enum.sum(v)} end)

Does it produce the expected output? With the output being:

[{'pen', 8}, {'soap', 3}]

Outputs step-by-step:

iex(1)> items = [%{name: 'soap', price: 50, quantity: 3}, %{name: 'pen', price: 10, quantity: 2}, %{name: 'pen', price: 14, quantity: 6}]
[
  %{name: 'soap', price: 50, quantity: 3},
  %{name: 'pen', price: 10, quantity: 2},
  %{name: 'pen', price: 14, quantity: 6}
]

iex(2)> items |> Enum.group_by(& &1.name)
%{
  'pen' => [
    %{name: 'pen', price: 10, quantity: 2},
    %{name: 'pen', price: 14, quantity: 6}
  ],
  'soap' => [%{name: 'soap', price: 50, quantity: 3}]
}

iex(3)> items |> Enum.group_by(& &1.name, & &1.quantity)
%{'pen' => [2, 6], 'soap' => [3]}

iex(4)> items |> Enum.group_by(& &1.name, & &1.quantity) |> Enum.map(fn {name, quantities} -> {name, Enum.sum(quantities)} end)
[{'pen', 8}, {'soap', 3}]

iex(5)> items |> Enum.group_by(& &1.name, & &1.quantity) |> Enum.map(fn {name, quantities} -> {name, Enum.sum(quantities)} end) |> Map.new()
%{'pen' => 8, 'soap' => 3}
1 Like

Or in one traversal:

Enum.reduce(items, %{}, fn %{name: name, quantity: quantity}, acc ->
  Map.update(acc, name, quantity, &(&1 + quantity))
end)

Alternatively with the new for reduce:

for %{name: name, quantity: quantity} <- items, reduce: %{} do
  acc -> Map.update(acc, name, quantity, &(&1 + quantity))
end
7 Likes

Thanks. This looks good