Number of each unique value in an array

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

You can use countmap from StatsBase.jl

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
6 Likes

With no other packages, you could do something like

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

5 Likes

A slight enhancement:

[element => count(==(element),a) for element in unique(a) ]
1 Like

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> 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> 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

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 :laughing: but it may still be of interest to someone

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)
1 Like