for (index,unique) in enumerate(unique_elements)
all_index = findall(x->x==unique,y)
index_array[index] = all_index
end
index_array = [[1,8],[2,3],[4],[5,6],[7]]
Is there any way that is faster than this ? My problem is that the y array im actually working with has 3 000 000 elements and this therefore takes over a minute to do… So I was wondering if anyone had an idea on how to optimize this.
Thanks !!
Your implementation is O(n^2) where n is the length of the array, so you can definitely do better by making a single pass over the array and using e.g. a dictionary (hash table) as in some of the posts I linked above.
function dict_unique_indx(y)
index_dict = Dict{eltype(y), Vector{Int64}}()
for (indx, element) in pairs(y)
if haskey(index_dict, element)
push!(index_dict[element], indx)
else
index_dict[element] = [indx]
end
end
index_dict
end