Collapse array of sets to single set in Julia

Viewed 230

How do I collapse an array of sets to a single set containing all unique set elements in the following way?

Array_of_sets = Set{String}[Set(["EUR", "GBP", "USD"]), Set(["AUD"])]
# do something to Array_of_sets which produces
Set{String}(["EUR", "GBP", "USD", "AUD"])

union, vcat and unique do not seem to work in this case.

2 Answers

So sets are already unique:

julia> S = Set(["a", "b"])
 Set(["b", "a"])
julia> push!(S, "a")
 Set(["b", "a"])

Therefore, no unique needed:

julia> A = Set{String}[Set(["EUR", "GBP", "USD"]), Set(["EUR", "AUD"])]
julia> reduce(union!, A)
 Set(["EUR", "GBP", "AUD", "USD"])

I would convert the sets to arrays, and vcat them:

Array_of_sets = Set{String}[Set(["EUR", "GBP", "USD"]), Set(["AUD"])]
Array_of_arrays = map(collect, Array_of_sets)
Set(vcat(Array_of_arrays...))
> Set{String}(["EUR", "GBP", "USD", "AUD"])

Edit: But then I'm not as clever as @stillearningsomething

Related