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
- how many items occur for “pen” and “soap” or what if it gives me maximum value occurs in a particular key?
Most Liked
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
7
idi527
![]()
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
Last Post!
siddhant3030
Popular in Questions
Hello, how can I check the Phoenix version ?
Thanks !
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
I would like to know what is the best IDE for elixir development?
New
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
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
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
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
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
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
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
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
...
New
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...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









