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:
object GfG
{
def main(args : Array[String])
{
val m 1 = List( 1 , 1 , 3 , 3 , 5 , 2 )
val res = m 1 .distinct
println(res)
}
}
|
Example #2:
object GfG
{
def main(args : Array[String])
{
val m 1 = List( 1 , 1 , 3 , 3 , 3 , 5 , 4 , 5 , 2 )
val res = m 1 .distinct
println(res)
}
}
|
Output:
List(1, 3, 5, 4, 2)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...