Related Articles

Related Articles

Scala List distinct() method with example
  • Last Updated : 26 Jul, 2019

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
      
    }
}

chevron_right


Output:

List(1, 3, 5, 2)

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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)
      
    }
}

chevron_right


Output:

List(1, 3, 5, 4, 2)



My Personal Notes arrow_drop_up
Recommended Articles
Page :