Scala List distinct() method with example
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 m 1 = List( 1 , 1 , 3 , 3 , 5 , 2 ) // Applying distinct method val res = m 1 .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 m 1 = List( 1 , 1 , 3 , 3 , 3 , 5 , 4 , 5 , 2 ) // Applying distinct method val res = m 1 .distinct // Displays output println(res) } } |
Output:
List(1, 3, 5, 4, 2)