Open In App

Scala Set intersect() method with example

Last Updated : 18 Oct, 2019
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report
The intersect() method is utilized to compute the intersection between two sets.
Method Definition: def intersect(that: Set[A]): Set[A] Return Type: It returns a set containing the elements present in both the sets.
Example #1: Scala
// Scala program of intersect() 
// method 

// Creating object 
object GfG 
{ 

    // Main method 
    def main(args:Array[String]) 
    { 
        // Creating a set 
        val s1 = Set(1, 2, 3, 4, 5) 
        
        val s2 = Set(11, 12, 13, 4, 5) 
        
        // Applying intersect method 
        val s3 = s1.intersect(s2) 
        
        s3.foreach(x => println(x))
    } 
} 
Output:
5
4
Example #2: Scala
// Scala program of intersect() 
// method 

// Creating object 
object GfG 
{ 

    // Main method 
    def main(args:Array[String]) 
    { 
        // Creating a set 
        val s1 = Set(1, 2, 3, 4, 5) 
        
        val s2 = Set(11, 2, 3, 4, 5) 
        
        // Applying intersect method 
        val s3 = s1.intersect(s2) 
        
        s3.foreach(x => println(x))
    } 
} 
Output:
5
2
3
4

Similar Reads