Q:

Write a Scala program to get the difference between two given lists

0

Write a Scala program to get the difference between two given lists.

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 list1 = List("Red","Blue","Blue","Green","Black")
   val list2 = List("Blue","White")
   println("Original lists")
   println(list1)
   println(list2)
   println("Difference of the said two lists(list1-list2):")
   val temp = list2.toSet
   val result = list1.filterNot(temp)
   println(result)   
   println("Difference of the said two lists(list2-list1):")
   val temp1 = list1.toSet
   val result1 = list2.filterNot(temp1)
   println(result1)   
  }
}

Sample Output:

Original lists
List(Red, Blue, Blue, Green, Black)
List(Blue, White)
Difference of the said two lists(list1-list2):
List(Red, Green, Black)
Difference of the said two lists(list2-list1):
List(White)

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