union
union(s1,s2...) ∪(s1,s2...)
Construct the union of two or more sets. Maintains order with arrays.
Examples
julia> set1 = Set(3, 4, 5, 6)
set2 = Set(1, 3, 4)
union(set1,set2)
Set{Int64}({4,3,5,6,1})
julia> a = Set("Hello")
Set{Char}({'e','H','l','o'})
julia> b = Set([10:-1:6])
Set{Int64}({7,9,10,8,6})
julia> union(a,b)
Set{Integer}({7,9,10,'e','H','l',8,'o',6})
julia> union([1, 2, 3], [3, 4, 5])
5-element Array{Int64,1}:
1
2
3
4
5
julia> union([1, 2, 3], [3, 4, 5], [6, 7, 8])
8-element Array{Int64,1}:
1
2
3
4
5
6
7
8
julia> union([1, 2, 3], [3, 4, 5], [5, 6, 7])
7-element Array{Int64,1}:
1
2
3
4
5
6
7
julia> union([1, 2, 3], [4, 5, 6])
6-element Array{Int64,1}:
1
2
3
4
5
6
julia> union(["apple", "banana"], ["banana", "orange"])
3-element Array{String,1}:
"apple"
"banana"
"orange"
julia> union(Set([1, 2, 3]), Set([3, 4, 5]))
Set{Int64} with 5 elements:
4
2
3
5
1
Common mistake example:
julia> union([1, 2, 3], 4, 5)
ERROR: MethodError: no method matching union(::Array{Int64,1}, ::Int64, ::Int64)
In this example, the union
function is expecting sets or arrays as arguments and not individual elements. Make sure to pass sets or arrays as arguments to the union
function.
See Also
complement, complement!, intersect, intersect!, issubset, selectperm, selectperm!, Set, setdiff, setdiff!, symdiff, union, union!,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.