object SecondMostFrequentChar_Scala {
def main(args: Array[String]): Unit = {
val str = "bbaddddccce"
// Step 1: Generate frequency map
val frequencyMap = str.groupBy(identity).mapValues(_.length)
// Step 2: Sort the map by frequency in descending order
val sortedByFrequency = frequencyMap.toSeq.sortWith(_._2 > _._2)
// Step 3: Extract the second most frequent character
if (sortedByFrequency.length > 1) {
val secondMostFrequent = sortedByFrequency(1)._1
println(s"The second most frequent character is: '$secondMostFrequent'")
} else {
println("The string does not have enough unique characters.")
}
}
}
/*
run:
The second most frequent character is: 'c'
*/