Open In App

Scala List distinct() method with example

Last Updated : 26 Jul, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The distinct() method is utilized to delete the duplicate elements from the stated list.
Method Definition: def distinct: List[A] Return Type: It returns a new list of elements without any duplicates.
Example #1: Scala
// Scala program of distinct()
// method

// Creating object
object GfG
{ 

    // Main method
    def main(args:Array[String])
    {
    
        // Creating a list
        val m1 = List(1, 1, 3, 3, 5, 2)
        
        // Applying distinct method
        val res = m1.distinct
        
        // Displays output
        println(res)
    
    }
}
Output:
List(1, 3, 5, 2)
Example #2: Scala
// Scala program of distinct()
// method

// Creating object
object GfG
{ 

    // Main method
    def main(args:Array[String])
    {
    
        // Creating a list
        val m1 = List(1, 1, 3, 3, 3, 5, 4, 5, 2)
        
        // Applying distinct method
        val res = m1.distinct
        
        // Displays output
        println(res)
    
    }
}
Output:
List(1, 3, 5, 4, 2)

Similar Reads