How to use the intersect() method of a set in Julia

Overview

The intersect method is used to find the intersectionThis returns a new set of elements that are common in the provided sets. of the given sets.

Syntax

intersect(s, itrs...)

Arguments

This method takes the sets as an argument.

Return value

This method returns a set that contains the common elements of the provided argument sets.

Code example

The code given below demonstrates how we can use the intersect method:

# intersection of two set
println(intersect(Set([1, 2]), Set([1, 4])))
# intersection of two set
println(intersect(Set([1, 2]), Set([3, 4])))
# intersection of two set
println(intersect(Set([1, 2]), Set([1, 2])))

Code explanation

  • Line 2: We use the intersect method to find the common elements between the sets [1,2] and [1,4]. The common elements between the two sets is 1, so a new set with [1] as an element is returned.

  • Line 5: We use the intersect method to find the common elements between the sets [1,2] and [3,4]. There is no common element between these two sets, so an empty set is returned.

  • Line 8: We use the intersect method to find the common elements between the sets [1,2] and [1,2]. The common elements between the two sets are 1 and 2, so a new set with [1, 2] as its elements is returned.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources