intersect!
intersect!(s1, s2)
Intersects sets s1
and s2
and overwrites the set s1
with the result. If needed, s1
will be expanded to the size of s2
.
Examples
-
Intersect two arrays:
julia> arr1 = [1, 2, 3, 4, 5]; julia> arr2 = [4, 5, 6, 7, 8]; julia> intersect!(arr1, arr2) 2-element Array{Int64,1}: 4 5
This example finds the common elements between
arr1
andarr2
and overwritesarr1
with the result. -
Intersect two sets:
julia> set1 = Set([1, 2, 3, 4, 5]); julia> set2 = Set([4, 5, 6, 7, 8]); julia> intersect!(set1, set2) Set{Int64} with 2 elements: 4 5
It performs intersection on two sets
set1
andset2
and modifiesset1
with the common elements. - Intersect arrays and sets:
julia> arr = [1, 2, 3, 4, 5]; julia> set = Set([4, 5, 6, 7, 8]); julia> intersect!(arr, set) 2-element Array{Int64,1}: 4 5
It finds the intersection between an array
arr
and a setset
and updatesarr
with the common elements.
Common mistake example:
julia> arr = [1, 2, 3, 4, 5];
julia> intersect!(arr, [4, 5, 6, 7, 8])
ERROR: MethodError: no method matching intersect!(::Array{Int64,1}, ::Array{Int64,1})
In this example, the second argument provided is an array instead of a set. The intersect!
function expects both arguments to be of type Set
. Make sure to pass sets when using intersect!
.
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.