Scala Programming: Find the maximum occurring character in a string
Scala Programming String Exercise-15 with Solution
Write a Scala program to find the maximum occurring character in a string.
Sample Solution:
Scala Code:
object Scala_String {
def MaxOccuringChar(str1: String): Char = {
val N = 256;
val ctr = new Array[Int](N);
val l = str1.length();
for (i <- 0 to l - 1)
ctr(str1.charAt(i)) = ctr(str1.charAt(i)) + 1;
var max = -1;
var result =' ';
for (i <- 0 to l - 1) {
if (max < ctr(str1.charAt(i))) {
max = ctr((str1.charAt(i)))
result = str1.charAt(i)
}
}
result
}
def main(args: Array[String]): Unit = {
val str1 ="test string"
println("The given string is: " + str1)
println(
"Maximum occurring character of the said string is: " + MaxOccuringChar(str1)
);
val str2 ="Scala"
println("The given string is: " + str2)
println(
"Maximum occurring character of the said string is: " + MaxOccuringChar(str2)
);
}
}
Sample Output:
The given string is: test string Maximum occurring character of the said string is: t The given string is: Scala Maximum occurring character of the said string is: a
Scala Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous:Write a Scala program to print after removing duplicates from a given string.
Next: Write a Scala program to reverse every word in a given string.
What is the difficulty level of this exercise?
- New Content published on w3resource:
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- React - JavaScript Library
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework