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
I would like to know what is the best IDE for elixir development?
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
Hi everyone,
I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New
Other popular topics
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Lets say I have map like this fetching from my database
%{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
What is the idiomatic way of matching for not nil in Elixir?
E.g.,
First way:
defp halt_if_not_signed_in(conn, signed_in_account) when...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]
15:22:35.803 [error] gen_event {lager_file_backend...
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









