# Providing a method on Seqs to count instances of each unique item?

**URL:** <https://users.scala-lang.org/t/providing-a-method-on-seqs-to-count-instances-of-each-unique-item/4829>\
**Category:** Libraries\
**Created:** [July 13, 2019, 3:51am UTC](https://users.scala-lang.org/t/providing-a-method-on-seqs-to-count-instances-of-each-unique-item/4829 "2019-07-13T03:51:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![julianzucker](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@julianzucker](https://users.scala-lang.org/u/julianzucker)\
**Post date:** [July 13, 2019, 3:51am UTC](https://users.scala-lang.org/t/providing-a-method-on-seqs-to-count-instances-of-each-unique-item/4829/1 "2019-07-13T03:51:20Z")

</div>

Hi,

I’ve been using Scala for a bit over a year now, and I often find myself writing:

`seq.groupBy(x => x).view.mapValues(_.length)`

Is this the best way to count the number of each item in a Seq? If not, please let me know the better way. However, if this is indeed the best, it would be a kindness to include a method on Seq that computes this for users in one method call, perhaps `seq.itemCounts()`.

Cheers,  
Julian

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [July 13, 2019, 3:09pm UTC](https://users.scala-lang.org/t/providing-a-method-on-seqs-to-count-instances-of-each-unique-item/4829/2 "2019-07-13T15:09:33Z")

</div>

Honestly, I suspect it’s too specialized to be likely to show up on Seq proper. (I can’t recall ever needing such a function myself.)

But I’d suggest adding it yourself: it’s not hard to make a class (or at worst a tiny library) with something like:

```auto
implicit class RichSeq[T](seq: Seq[T]) {
  def itemCounts: Map[T, Int] = seq.groupBy(x => x).view.mapValues(_.length)
}

```

---

<div class="post-metadata">

**Author:** ![tyohDeveloper](https://users.scala-lang.org/user_avatar/users.scala-lang.org/tyohdeveloper/32/1094_2.png) [@tyohDeveloper](https://users.scala-lang.org/u/tyohDeveloper)\
**Post date:** [July 13, 2019, 4:19pm UTC](https://users.scala-lang.org/t/providing-a-method-on-seqs-to-count-instances-of-each-unique-item/4829/3 "2019-07-13T16:19:59Z")

</div>

It is a smallish Seq, toSet.length will be faster than most everything.

---

<div class="post-metadata">

**Author:** ![Russ](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/r/e36b37/32.png) [@Russ](https://users.scala-lang.org/u/Russ)\
**Post date:** [July 13, 2019, 4:32pm UTC](https://users.scala-lang.org/t/providing-a-method-on-seqs-to-count-instances-of-each-unique-item/4829/4 "2019-07-13T16:32:01Z")

</div>

Am I missing something, or can you just use

seq.map(\_.length)

---

<div class="post-metadata">

**Author:** ![julianzucker](https://users.scala-lang.org/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@julianzucker](https://users.scala-lang.org/u/julianzucker)\
**Post date:** [July 13, 2019, 4:45pm UTC](https://users.scala-lang.org/t/providing-a-method-on-seqs-to-count-instances-of-each-unique-item/4829/5 "2019-07-13T16:45:31Z")

</div>

Thanks, I suppose I can do this. I run into this problem a lot on various research projects - suppose you are running a simulation, and the simulation returns an outcome, and you want to count how many times each outcome occurred, etc. It is probably less useful for enterprise development. I will make use of this implicit, thanks!

---

<div class="post-metadata">

**Author:** ![jducoeur](https://users.scala-lang.org/user_avatar/users.scala-lang.org/jducoeur/32/462_2.png) [@jducoeur](https://users.scala-lang.org/u/jducoeur)\
**Post date:** [July 13, 2019, 4:56pm UTC](https://users.scala-lang.org/t/providing-a-method-on-seqs-to-count-instances-of-each-unique-item/4829/6 "2019-07-13T16:56:44Z")

</div>

> [@Russ](#):
>
> Am I missing something, or can you just use
> 
> seq.map(\_.length)

That sounds how long each individual item is. What @julianzucker is looking for is the number of copies of each distinct item there are…
