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

  • برمجة سكالا

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

الأجوبة

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)
هل كان المحتوى مفيد؟

معلومات ذات صلة

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...