• + 0 comments

    Scala:

    object Solution {
        import scala.io.StdIn.{readInt, readLine}
    
        def main(args: Array[String]): Unit = {
            val A = readList
            val B = readList
            println(missingVals(A, B).sorted.mkString(" "))
        }
    
        def readList: List[Int] = {
            val _ = readInt
            readLine.split(' ').map(_.toInt).toList
        }
    
        def missingVals[T](A: List[T], B: List[T]): List[T] = {
            val (mapA, mapB) = (countMap(A), countMap(B))
            (mapA.keySet | mapB.keySet)
                .filterNot(n => mapA(n) == mapB(n))
                .toList
        }
    
        def countMap[T](l: List[T]): Map[T, Int] = {
            val m = Map.empty[T, Int].withDefaultValue(0)
            l.foldLeft(m)((m, e) => m + (e -> (m(e) + 1)))
        }
    }