Q:

Write a Scala program to remove duplicates from a given list

0

Write a Scala program to remove duplicates from a given list

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

object Scala_List
{
def main(args: Array[String]): Unit = 
 {
   val nums = List(1, 3, 5, 2, 7, 9, 11, 5, 2, 14, 12, 3)
   println("Original list:")
   println(nums)   
   val result1 = nums.distinct
   println("Unique elements of the said list:")
   println(result1)
   val chars = List("a", "a", "b", "c", "d", "c", "e", "f")
   println("Original list:")
   println(chars)   
   val result2 = chars.distinct
   println("Unique elements of the said list:")
   println(result2)    
  }
}

Sample Output:

Original list:
List(1, 3, 5, 2, 7, 9, 11, 5, 2, 14, 12, 3)
Unique elements of the said list:
List(1, 3, 5, 2, 7, 9, 11, 14, 12)
Original list:
List(a, a, b, c, d, c, e, f)
Unique elements of the said list:
List(a, b, c, d, e, f)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now