How to group and count occurrences of values in Elixir's list

Article autor
September 9, 2025
How to group and count occurrences of values in Elixir's list
Elixir Newsletter
Join Elixir newsletter

Subscribe to receive Elixir news to your inbox every two weeks.

Oops! Something went wrong while submitting the form.
Elixir Newsletter
Expand your skills

Download free e-books, watch expert tech talks, and explore open-source projects. Everything you need to grow as a developer - completely free.

Table of contents

If you ever had to count occurrences of values in Elixir's list, this short post might be helpful for you!

Let's assume that the input contains a list of people names:

people = [
  %{name: "John"},
  %{name: "Tom"},
  %{name: "John"},
  %{name: "David"}
]

Our goal here is to count occurrences of names so that in the end we'll get this summary:

%{"David" => 1, "John" => 2, "Tom" => 1}

In Elixir, it's super easy! You can use Enum.frequencies_by/2 to achieve that in a simple one-liner:

iex > Enum.frequencies_by(people, & &1.name)
%{"David" => 1, "John" => 2, "Tom" => 1}

Work with a team that keeps learning and building better software every day.

Related posts

Dive deeper into this topic with these related posts

No items found.

You might also like

Discover more content from this category

Style spacing between repeated elements in CSS using flex gap

It's a pretty common scenario - you have to place a few elements in equal distances. E.g. unordered list items.

How to contain a fixed positioned element

It's easy to contain absolute positioned elements. Things get a little trickier when you want to contain a fixed positioned element without changing its stylings.

How to override Kernel macros

The macro mechanism in Elixir is not only an interesting metaprogramming feature - in fact, it is at the language's very core. And the more awesome fact is that, using macros, you can override the algorithm of defining functions itself!