Iterating through a nested map without changing the structure


I am having a map from which I want to extract values by the maximum length of values.

map = 
  "A" => [
    {:b, "B", 1},
    {:b, "C", 1}
  "B" => [
    {:b, "D", 1}
  "C" => [
    {:b, "E", 1},
    {:b, "F", 1}

Here is my code:

 Enum.max_by(map, fn {key, value} -> length(value) end)

which gives me

{"A", [{:b, "B", 1}, {:b, "C", 1}]}

while I need to get a list of both key values that have maximum values. In this case, this should be the output

   {"A", [{:b, "B", 1}, {:b, "C", 1}]}, 
   {"C", [{:b, "E", 1}, {:b, "F", 1}]}


Enum.max_by returns at max 1 element =)
If you want to do it in a single cycle you probably want to do some custom reduce like

Enum.reduce(map, [], fn 
  item, [] -> [item]
  {_, val} = item, [{_, acc_val} | _] when length(val) > length(acc_val) -> [item]
  {_, val} = item, [{_, acc_val} | _] = acc when length(val) == length(acc_val) -> [item | acc]
  _, acc -> acc

P. S. naming is hard :neutral_face:


Seems that max_by won’t work for you here, because:

If multiple elements are considered maximal, the first one that was found is returned.

So you need something like:

{count, values} =
  |> Enum.group_by(fn {_key, values} -> length(values) end)
  |> Enum.max_by(fn {count, _values} -> count end)

Yeah it makes sense now. max_by will output the only instance maximum output. Enum.reduce seems like a better option.

1 Like

Thats right … as @RudManusachi mentioned as well.

To be honest, if Enum.reduce is a better option or not - I’m not sure :grinning_face_with_smiling_eyes:

I like the example of @stefanchrobot more, because it looks simpler and reads better =)

1 Like