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