Simple Scala with custom Ordering and Sliding Window
Code\nscala []\nobject Solution {\n    def countOccurrences(a: Array[Int]) : Map[Int, Int] = a.groupMapReduce[Int, Int](identity)(_ => 1)(_ + _)\n    def sortOc
# Code\n```scala []\nobject Solution {\n    def countOccurrences(a: Array[Int]) : Map[Int, Int] = a.groupMapReduce[Int, Int](identity)(_ => 1)(_ + _)\n    def sortOccurrenceCountPairs(m: Map[Int, Int]): Seq[(Int, Int)] = m.view.toSeq.sortWith(\n        (_, _) match {\n            case (e1, e2) if e1._2 > e2._2 => true\n            case (e1, e2) if e1._2 < e2._2 => false\n            case (e1, e2) if e1._1 > e2._1 => true\n            case _ => false\n        }\n    )\n    def computeXSum(a: Array[Int], x: Int) : Int = {\n        // Count the occurrences of all elements in the array.\n        val occurrenceCounts: Map[Int, Int] = countOccurrences(a)\n        // Keep only the occurrences of the top x most frequent elements.\n        // If two elements have the same number of occurrences, the element with the bigger value is considered more frequent.\n        val sortedOccurrenceCountPairs: Seq[(Int, Int)] = sortOccurrenceCountPairs(occurrenceCounts)\n        // Calculate the sum of the resulting array.\n        val answer : Int = sortedOccurrenceCountPairs.take(x).map[Int]((k,v) => k * v).sum\n        answer\n    }\n    def findXSum(nums: Array[Int], k: Int, x: Int): Array[Int] = {\n        val windows: Iterator[Array[Int]] = nums.sliding(k)\n        val answers : Array[Int] = windows.map(computeXSum(_, x)).toArray\n        answers\n    }\n}\n```
['Scala']