# Count various patterns simultaneously

**URL:** <https://discourse.julialang.org/t/count-various-patterns-simultaneously/73066>\
**Category:** General Usage\
**Tags:** question, iterators, count, splitapplycombine\
**Created:** [December 14, 2021, 10:33am UTC](https://discourse.julialang.org/t/count-various-patterns-simultaneously/73066 "2021-12-14T10:33:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ludwig-austermann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludwig-austermann/32/11093_2.png) [@ludwig-austermann](https://discourse.julialang.org/u/ludwig-austermann)\
**Post date:** [December 14, 2021, 10:33am UTC](https://discourse.julialang.org/t/count-various-patterns-simultaneously/73066/1 "2021-12-14T10:33:55Z")

</div>

I was just wondering if there is a nicer way to do practically this:

```julia
reduce(
    (d, c) -> (d[c] = get(d, c, 0) + 1; d), "ACCACD", init=Dict{Char, Int}()
)

```

hence, to do a non-binary count

```julia
s = "ACCACD"
Dict(
    c => count(==(c), s) for c in distinct(s)
    # or equally: `count(c, s)`
)

```

while only iterating once.

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [December 14, 2021, 10:46am UTC](https://discourse.julialang.org/t/count-various-patterns-simultaneously/73066/2 "2021-12-14T10:46:36Z")

</div>

This is simply a group + count operation. So:

```julia
julia> using SplitApplyCombine

julia> groupcount(s)
3-element Dictionaries.Dictionary{Char, Any}
 'A' │ 2
 'C' │ 3
 'D' │ 1

```

---

<div class="post-metadata">

**Author:** ![ludwig-austermann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ludwig-austermann/32/11093_2.png) [@ludwig-austermann](https://discourse.julialang.org/u/ludwig-austermann)\
**Post date:** [December 14, 2021, 11:00am UTC](https://discourse.julialang.org/t/count-various-patterns-simultaneously/73066/3 "2021-12-14T11:00:37Z")

</div>

Oh, that seems like a simple and nice solution, I hadn’t heard of that package. Thanks!

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 14, 2021, 11:42am UTC](https://discourse.julialang.org/t/count-various-patterns-simultaneously/73066/4 "2021-12-14T11:42:52Z")

</div>

For the example provided, `countmap()` in StatsBase seems to be \>200 times faster than `groupcount()` in SplitApplyCombine:

```julia
using StatsBase
s = "ACCACD"
countmap(s)

@btime countmap($s) # 110 ns (4 allocations: 480 bytes)
@btime groupcount($s) # 26.7 μs (85 allocations: 11.59 KiB)

```

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [December 14, 2021, 1:25pm UTC](https://discourse.julialang.org/t/count-various-patterns-simultaneously/73066/5 "2021-12-14T13:25:45Z")

</div>

Strange, I see a much faster performance of `groupcount` compared to yours:

```julia
julia> @btime groupcount($s)
  366.199 ns (8 allocations: 704 bytes)

```

with StatsBase at 150 ns on my laptop.

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 14, 2021, 1:34pm UTC](https://discourse.julialang.org/t/count-various-patterns-simultaneously/73066/6 "2021-12-14T13:34:10Z")

</div>

No clue of what is going on. I’ve repeated it several times with similar results. For the record:

```julia
Win11 Julia 1.7
StatsBase v0.33.13
SplitApplyCombine v1.2.0

```
