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

: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}

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement