'Adding String and Array[String] to a Array[String] by reassignment

I'm new to scala. I am trying to accomplish this.

e._1 is a Buffer with values like("India, Canada, USA, Russia") e._2 is a similar buffer with almost same values like ("India, Canada, Mexico, France")

When they are not equal. I'm trying to collect those values from e._1 to a new String, array or list. and print the string or array or list with the exception.

var missingPos = Array[String]()

for (e <- sms) {
if(!(e._1.equals(e._2))){
missingPos = (missingPos + e._1) 
throw new RuntimeException(s"Missing positions $missingPos")

So in the console. It should print Found exception: Missing positions USA, Russia

please help me achieve this.

Currently I'm getting error because I'm trying add the string e._1 to an Array. type mismatch.



Solution 1:[1]

There is no need to use mutability at all.

You only need to use proper data structures and higher order functions.

val inputValues = List("India, Canada, USA, Russia")
val reference = Set("India, Canada, Mexico, France")

val difference = inutValues.filterNot(reference.contains)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Luis Miguel Mejía Suárez