Open In App

Scala List distinct() method with example

Last Updated : 26 Jul, 2019
Improve
Improve
Like Article
Like
Save
Share
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 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 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)


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads