import scala.collection.mutable class FreqStack() // Use constructor to initialize the FreqStack object { // Declare a FreqStack class containing frequency and group hashmaps // and maxFrequency integer var frequency = mutable.HashMap[String, Int]().withDefaultValue(0) var group = mutable.HashMap[Int, mutable.Stack[String]]() var maxFrequency = 0 // Use push function to push the showName into the FreqStack def push(showName: String): Unit = { // Get the frequency for the given showName and // increment the frequency for the given showName val f = frequency(showName) + 1 frequency.put(showName, f) // Check if the maximum frequency is lower that the new frequency // of the given show if (f > maxFrequency) maxFrequency = f // Save the given showName for the new calculated frequency if(!group.contains(f)){ group(f) = new mutable.Stack[String]() } group(f).push(showName) } // Use the pop function to pop the showName from the FreqStack def pop: String = { var show = "" if (maxFrequency > 0) { // Fetch the top of the group[maxFrequency] stack and // pop the top of the group[maxFrequency] stack show = group(maxFrequency).pop // Decrement the frequency after the show has been popped frequency.put(show, frequency(show) - 1) if (group(maxFrequency).size == 0) maxFrequency -= 1 } show } } object Solution { def main(args: Array[String]): Unit = { val obj = new FreqStack obj.push("Queen's Gambit") obj.push("Teen Wolf") obj.push("Queen's Gambit") obj.push("Teen Wolf") obj.push("Bigderton") obj.push("Queen's Gambit") for (i <- 0 until 7) { println("...User navigates to the browser...") println("Continue Watching :" + obj.pop) println() } } }