intersect
intersect(s1,s2...) ∩(s1,s2)
Construct the intersection of two or more sets. Maintains order and multiplicity of the first argument for arrays and ranges.
Examples
julia> set1 = Set(3, 4, 5, 6)
set2 = Set(1, 3, 4)
intersect(set1, set2)
Set{Int64}({4,3})
julia> set1 = Set(3, 4, 5, 6)
set2 = Set(1, 2)
intersect(set1, set2)
Set{Int64}({})
julia> a = Set([0:3:30]); # table of 3
julia> b = Set([0:5:50]); # table of 5
julia> intersect(a, b)
Set{Int64}({0,15,30})
-
Find the intersection of 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 5This example finds the common elements between
arr1andarr2using theintersectfunction. -
Find the intersection of multiple arrays:
julia> arr1 = [1, 2, 3, 4, 5]; julia> arr2 = [4, 5, 6, 7, 8]; julia> arr3 = [3, 4, 5, 9, 10]; julia> intersect(arr1, arr2, arr3) 2-element Array{Int64,1}: 4 5In this example, the
intersectfunction is used to find the common elements amongarr1,arr2, andarr3. - Intersect two sets with different element types:
julia> set1 = Set([1, 2, 3, 4]); julia> set2 = Set([3.0, 4.0, 5.0]); julia> intersect(set1, set2) Set{Float64} with 2 elements: 3.0 4.0The
intersectfunction can also be used to find the common elements between sets of different element types.
Common mistake example:
julia> intersect([1, 2, 3], (3, 4, 5))
ERROR: MethodError: no method matching intersect(::Array{Int64,1}, ::Tuple{Int64,Int64,Int64})
In this example, the intersect function is used with an array and a tuple. However, the intersect function does not support intersection between arrays and tuples. Make sure to provide valid arguments to the intersect function that are compatible with each other.
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.