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}

Related posts

Dive deeper into this topic with these related posts

No items found.

You might also like

Discover more content from this category

How to set default value in JavaScript’s Destructuring

Did you know that it's possible to set default value in Javascript object destructuring?

How to deal with timeout issue when debugging Phoenix app

There is a common scenario: You'd like to debug your Phoenix app with break!/4 or IEx.pry/0. Everything works fine, until... Phoenix server throws a timeout error statement.

How to run tests in Elixir IEx shell

Hey! Have you ever wondered about tests running inside the IEx shell? For a long time, I was convinced that it’s not really possible. And as it turns out - that’s not really straightforward. You won’t easily find information about that in the documentation.