How to remove the letters from word1 if they do not exist in word2 with Scala

1 Answer

0 votes
object CommonLettersRemover {
  def removeNonCommonLetters(word1: String, word2: String): String = {
    word1.filter(word2.contains)
  }

  def main(args: Array[String]): Unit = {
    val word1 = "forest"
    val word2 = "tor"

    val result = removeNonCommonLetters(word1, word2)
    println(result) 
  }
}
 
 
  
/*
run:
 
ort
 
*/

 



answered Jul 10, 2025 by avibootz
...