siddhant3030

siddhant3030

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?

Most Liked

michalmuskala

michalmuskala

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
idi527

idi527

:waving_hand:

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}

Last Post!

siddhant3030

siddhant3030

Thanks. This looks good

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

We're in Beta

About us Mission Statement