# Number of each unique value in an array

**URL:** <https://discourse.julialang.org/t/number-of-each-unique-value-in-an-array/58627>\
**Category:** General Usage\
**Created:** [April 5, 2021, 3:58pm UTC](https://discourse.julialang.org/t/number-of-each-unique-value-in-an-array/58627 "2021-04-05T15:58:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgautam03](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tgautam03/32/20545_2.png) [@tgautam03](https://discourse.julialang.org/u/tgautam03)\
**Post date:** [April 5, 2021, 3:58pm UTC](https://discourse.julialang.org/t/number-of-each-unique-value-in-an-array/58627/1 "2021-04-05T15:58:37Z")

</div>

Hi, I’m new to Julia and converting my existing python code to julia.

I often use `np.unique` with `return_counts=True` to get all unique values and the number of times each one appears in an array. I’m trying to find an alternative to this in Julia but I can’t find anything. I hope someone can help me here.

Thanks

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [April 5, 2021, 4:04pm UTC](https://discourse.julialang.org/t/number-of-each-unique-value-in-an-array/58627/2 "2021-04-05T16:04:43Z")

</div>

You can use `countmap` from [StatsBase.jl](https://github.com/JuliaStats/StatsBase.jl)

```julia
julia> using StatsBase

julia> countmap([1, 1, 2, 3, 2, 1, 4, 1])
Dict{Int64, Int64} with 4 entries:
  4 => 1
  2 => 2
  3 => 1
  1 => 4

```

---

<div class="post-metadata">

**Author:** ![aramirezreyes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aramirezreyes/32/42573_2.png) [@aramirezreyes](https://discourse.julialang.org/u/aramirezreyes)\
**Post date:** [April 5, 2021, 5:19pm UTC](https://discourse.julialang.org/t/number-of-each-unique-value-in-an-array/58627/3 "2021-04-05T17:19:09Z")

</div>

With no other packages, you could do something like

```julia
julia> a = [1, 2, 1, 3, 4, 5, 6, 6, 7]
9-element Vector{Int64}:
 1
 2
 1
 3
 4
 5
 6
 6
 7

julia> [count(==(element),a) for element in unique(a)]
7-element Vector{Int64}:
 2
 1
 1
 1
 1
 2
 1

```

---

<div class="post-metadata">

**Author:** ![GHTaarn](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ghtaarn/32/216007_2.png) [@GHTaarn](https://discourse.julialang.org/u/GHTaarn)\
**Post date:** [March 26, 2024, 5:38am UTC](https://discourse.julialang.org/t/number-of-each-unique-value-in-an-array/58627/4 "2024-03-26T05:38:21Z")

</div>

A slight enhancement:

```julia
[element => count(==(element),a) for element in unique(a)]

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [March 26, 2024, 7:58am UTC](https://discourse.julialang.org/t/number-of-each-unique-value-in-an-array/58627/5 "2024-03-26T07:58:50Z")

</div>

If you just want to use the Basic features, here are a couple of alternatives to get what you’re looking for

[However, this is to underline that, in Julia, beyond the specific case and the appropriateness and validity of these proposals, sometimes it may be the case of trying to “build” the tools yourself (at least the simple ones)]

```julia
julia> a = [1, 2, 1, 3, 4, 5, 6, 6, 7]
9-element Vector{Int64}:
 1
 2
 1
 3
 4
 5
 6
 6
 7

julia> d=Dict{Int64, Int64}()
Dict{Int64, Int64}()

julia> foreach(k->d[k]=get!(d,k,0)+1,a)

julia> d
Dict{Int64, Int64} with 7 entries:
  5 => 1
  4 => 1
  6 => 2
  7 => 1
  2 => 1
  3 => 1
  1 => 2

```

```julia
julia> cu=zeros(Int,maximum(a))
7-element Vector{Int64}:
 0
 0
 0
 0
 0
 0
 0

julia> for i in eachindex(a)
           cu[a[i]]+=1
       end

julia> pairs(cu)
pairs(::Vector{Int64})(...):
  1 => 2
  2 => 1
  3 => 1
  4 => 1
  5 => 1
  6 => 2
  7 => 1

```

```julia
function cm(a)
    m,M=extrema(a)
    cu=zeros(Int,M-m+1)
    for i in eachindex(a)
        cu[a[i]-m+1]+=1
    end
    Pair.(eachindex(cu).+m.-1, cu)
end

function cmc(a)
     [element => count(==(element),a) for element in unique(a)]
end

```

I only realize now that this is a very old post 😆 but it may still be of interest to someone

```julia
julia> a=rand(1:100,10^6);

julia> @btime countmap(a);
  3.600 ms (13 allocations: 7.64 MiB)

julia> @btime cm(a);
  851.000 μs (2 allocations: 2.64 KiB)

julia> function cmc(a)
       [element => count(==(element),a) for element in unique(a)]       
       end
cmc (generic function with 1 method)

julia> @btime cmc(a);
  24.930 ms (15 allocations: 7.31 KiB)

```
