union!
union!(s, iterable)
Union each element of iterable
into set s
in-place.
Examples
-
Merge two sets in-place:
julia> set1 = Set([1, 2, 3]); julia> set2 = Set([3, 4, 5]); julia> union!(set1, set2) Set{Int64} with 5 elements: 4 2 3 5 1
This example merges
set2
intoset1
in-place, resulting in a set with all unique elements from both sets. -
Add elements from an array to a set:
julia> set = Set([1, 2, 3]); julia> arr = [3, 4, 5]; julia> union!(set, arr) Set{Int64} with 5 elements: 4 2 3 5 1
It adds the elements from the array
arr
to the setset
in-place. - Merge multiple sets into one:
julia> set1 = Set([1, 2, 3]); julia> set2 = Set([3, 4, 5]); julia> set3 = Set([5, 6, 7]); julia> union!(set1, set2, set3) Set{Int64} with 7 elements: 4 2 3 5 7 6 1
This example demonstrates how to merge multiple sets (
set2
andset3
) intoset1
in-place.
Common mistake example:
julia> set = Set([1, 2, 3]);
julia> union!(set, [3, 4, 5])
ERROR: MethodError: no method matching union!(::Set{Int64}, ::Array{Int64,1})
In this example, the union!
function is called with a set and an array. However, union!
expects the second argument to be an iterable, not an array directly. To fix this, wrap the array with []
to create an iterable:
julia> set = Set([1, 2, 3]);
julia> union!(set, [3, 4, 5])
Set{Int64} with 5 elements:
4
2
3
5
1
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.