# Unique! and count

**URL:** https://discourse.julialang.org/t/unique-and-count/73440
**Category:** New to Julia
**Created:** [December 21, 2021, 2:22pm UTC](https://discourse.julialang.org/t/unique-and-count/73440 "2021-12-21T14:22:47Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![hieun](https://avatars.discourse-cdn.com/v4/letter/h/8e8cbc/32.png) [@hieun](https://discourse.julialang.org/u/hieun)
#### Post date: [December 21, 2021, 2:22pm UTC](https://discourse.julialang.org/t/unique-and-count/73440/1 "2021-12-21T14:22:47Z")

</div>

Hi,  
I’m trying to determine unique counts of values in an array  
as an example, given the following array  
data = [‘a’, ‘b’, ‘a’, ‘c’]  
i wanna get: unique\_array = [‘a’, ‘b’, ‘c’] and count\_array = [2,1,1]  
in python I can do like this: unique\_array, count\_array = np.unique(data, return\_counts=True)  
and i can also solve with julia like this: unique\_array = unique!(data)  
but when count, I use: count(i=\>i==‘a’,data). I wonder if there are some other solutions in case I don’t know the value of data (a,b,c)

---

<div class="post-metadata">

### Author: ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)
#### Post date: [December 21, 2021, 2:27pm UTC](https://discourse.julialang.org/t/unique-and-count/73440/2 "2021-12-21T14:27:33Z")

</div>

Something like `count_array = [count(==(x), data) for x in unique_array]` should work, though this loops over the data many times so if you have a lot of data to crunch it might be worth to look at something smarter.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [December 21, 2021, 3:16pm UTC](https://discourse.julialang.org/t/unique-and-count/73440/3 "2021-12-21T15:16:48Z")

</div>

> [@hieun](#):
>
> I’m trying to determine unique counts of values in an array  
> as an example, given the following array

Sounds like you want [`countmap`](https://juliastats.org/StatsBase.jl/stable/counts/#StatsBase.countmap) in the [StatsBase.jl](https://github.com/JuliaStats/StatsBase.jl) package.

---

<div class="post-metadata">

### Author: ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)
#### Post date: [December 21, 2021, 5:23pm UTC](https://discourse.julialang.org/t/unique-and-count/73440/4 "2021-12-21T17:23:01Z")

</div>

Or

```julia
function uniquecount(data)
   unique_array = unique(data)
   counts = Dict(unique_array .=> 0)
   for (i, c) in enumerate(data)
      counts[c] += 1     
   end
   keys(counts), values(counts)
end

```

---

<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 21, 2021, 6:41pm UTC](https://discourse.julialang.org/t/unique-and-count/73440/5 "2021-12-21T18:41:20Z")

</div>

For data input as: `data = rand('a':'z', 1000)`, StatsBase’s `countmap()` (_including collecting keys and values_) seems to be 25% faster than the `count()` comprehension, and ~3x faster than `uniquecount()`.

---

<div class="post-metadata">

### Author: ![hieun](https://avatars.discourse-cdn.com/v4/letter/h/8e8cbc/32.png) [@hieun](https://discourse.julialang.org/u/hieun)
#### Post date: [December 22, 2021, 4:20pm UTC](https://discourse.julialang.org/t/unique-and-count/73440/6 "2021-12-22T16:20:59Z")

</div>

Thanks, I got it
