Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: How to return the number of unique elements in an Array

Tags:

julia

What's the function to return the number of unique elements in an Array in Julia?

In R you have length(unique(x)). I can do the same in Julia but there should be a more efficient way I think.

like image 329
xiaodai Avatar asked Oct 02 '19 03:10

xiaodai


Video Answer


1 Answers

If you want an exact answer length(unique(x)) is as efficient as it gets for general objects. If your values have a limited domain, eg UInt8, it may be more efficient to use a fixed size table. If you can accept an approximation, then you can use the HyperLogLog data structure / algorithm, which is implemented in the OnlineStats package:

https://joshday.github.io/OnlineStats.jl/latest/api/#OnlineStats.HyperLogLog

like image 149
StefanKarpinski Avatar answered Oct 05 '22 07:10

StefanKarpinski