Scala solution


  • 0
    W
    def intersect(nums1: Array[Int], nums2: Array[Int]): Array[Int] = {
        val r = scala.collection.mutable.ArrayBuffer.empty[Int]
        val numMap1 = scala.collection.mutable.HashMap[Int, Int](nums1.groupBy(i => i).mapValues(_.length).toSeq: _*)
        nums2.foreach { i =>
          numMap1.get(i) match {
            case Some(c) if c > 0 =>
              numMap1.update(i, c - 1)
              r += i
            case _ =>
          }
        }
        r.toArray
      }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.