Skip to content

Instantly share code, notes, and snippets.

@tk3369
Last active March 23, 2018 07:17
Show Gist options
  • Save tk3369/8aded8faabf9432b08f2921bf754a3a3 to your computer and use it in GitHub Desktop.
Save tk3369/8aded8faabf9432b08f2921bf754a3a3 to your computer and use it in GitHub Desktop.
groupby function that returns a Dict of indices
# see https://discourse.julialang.org/t/allocation-groupby/7505
function groupby(v::AbstractVector{T}) where T
d = Dict{T,Vector{Int}}()
i = 1
for x in v
if !haskey(d, x)
d[x] = [i]
else
push!(d[x], i)
end
i += 1
end
d
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment