Q:

Write a Scala program to find the common elements between two arrays of strings

0

Write a Scala program to find the common elements between two arrays of strings.

All Answers

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

object Scala_Array {
  import scala.collection.mutable.ArrayBuffer
  def main(args: Array[String]): Unit = {
    var array1 = Array("Python", "JAVA", "PHP", "C#", "C++", "SQL");
    var array2 =
      Array("MySQL", "SQL", "SQLite", "Oracle", "PostgreSQL", "DB2", "JAVA");
    println("Array elements are : ")
    for (c <- array1) {
      print(s"${c}, ")
    }
    println("\nSecond array:")
    for (c <- array2) {
      print(s"${c}, ")
    }

    val temp: ArrayBuffer[String] = ArrayBuffer.empty[String]
    for (i <- 0 to array1.length - 1) {
      for (j <- 0 to array2.length - 1) {
        if (array1(i).equals(array2(j))) {
          temp.append(array1(i));
        }
      }
    }
    println("\nCommon elements of the said two arrays:")
    for (c <- temp) {
      print(s"${c}, ")
    }
  }
}

Sample Output:

Array elements are : 
Python, JAVA, PHP, C#, C++, SQL, 
Second array:
MySQL, SQL, SQLite, Oracle, PostgreSQL, DB2, JAVA, 
Common elements of the said two arrays:
JAVA, SQL,

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