Below code will find the number of pairs exists in the array

    import java.io._
    import java.math._
    import java.security._
    import java.text._
    import scala.collection.mutable._ 
    import java.util.concurrent._
    import java.util.function._
    import java.util.regex._
    import java.util.stream._

    object Solution {

        def sockMerchant(n: Int, ar: Array[Int]): Int = {
        var pairs: HashMap[Int,Int] = HashMap.empty[Int,Int]
            var count = 0;
            for (a <- ar){
                if (pairs.contains(a)){
                    count += 1
                    pairs -= (a)
                }else{
                    pairs += (a -> a)
                }                        
            }
            return count
        }

        def main(args: Array[String]) {
            val stdin = scala.io.StdIn

            val printWriter = new PrintWriter(sys.env("OUTPUT_PATH"))

            val n = stdin.readLine.trim.toInt

            val ar = stdin.readLine.split(" ").map(_.trim.toInt)
            val result = sockMerchant(n, ar)

            printWriter.println(result)

            printWriter.close()
        }
    }