Return most occurring character in an input string
Here given code implementation process.
// Include header file
#include <iostream>
#include <string>
using namespace std;
/*
C++ Program for
Return most occurring character in an input string
*/
class Occurrence
{
public: char mostOccurrence(string text)
{
int n = text.length();
char ans = ' ';
int max = 0;
// Use to count frequency of characters
int count[256];
// Set initial frequency of possible characters
for (int i = 0; i < 256; ++i)
{
count[i] = 0;
}
// Counting the value of character element frequency
for (int i = 0; i < n; ++i)
{
// Increase frequency
count[text[i]]++;
if (count[text[i]] > max)
{
// Get resultant character
ans = text[i];
// Get the max occurring element frequency
max = count[text[i]];
}
}
return ans;
}
};
int main()
{
Occurrence *task = new Occurrence();
string input1 = "uxyzzxabxb";
string input2 = "apple";
// Test
cout << task->mostOccurrence(input1) << endl;
cout << task->mostOccurrence(input2) << endl;
return 0;
}
Output
x
p
/*
Java Program for
Return most occurring character in an input string
*/
public class Occurrence
{
public char mostOccurrence(String text)
{
int n = text.length();
char ans = ' ';
int max = 0;
// Use to count frequency of characters
int[] count = new int[256];
// Set initial frequency of possible characters
for (int i = 0; i < 256; ++i)
{
count[i] = 0;
}
// Counting the value of character element frequency
for (int i = 0; i < n; ++i)
{
// Increase frequency
count[text.charAt(i)]++;
if (count[text.charAt(i)] > max)
{
// Get resultant character
ans = text.charAt(i);
// Get the max occurring element frequency
max = count[text.charAt(i)];
}
}
return ans;
}
public static void main(String[] args)
{
Occurrence task = new Occurrence();
String input1 = "uxyzzxabxb";
String input2 = "apple";
// Test
System.out.println(task.mostOccurrence(input1));
System.out.println(task.mostOccurrence(input2));
}
}
Output
x
p
// Include namespace system
using System;
/*
Csharp Program for
Return most occurring character in an input string
*/
public class Occurrence
{
public char mostOccurrence(String text)
{
int n = text.Length;
char ans = ' ';
int max = 0;
// Use to count frequency of characters
int[] count = new int[256];
// Set initial frequency of possible characters
for (int i = 0; i < 256; ++i)
{
count[i] = 0;
}
// Counting the value of character element frequency
for (int i = 0; i < n; ++i)
{
// Increase frequency
count[text[i]]++;
if (count[text[i]] > max)
{
// Get resultant character
ans = text[i];
// Get the max occurring element frequency
max = count[text[i]];
}
}
return ans;
}
public static void Main(String[] args)
{
Occurrence task = new Occurrence();
String input1 = "uxyzzxabxb";
String input2 = "apple";
// Test
Console.WriteLine(task.mostOccurrence(input1));
Console.WriteLine(task.mostOccurrence(input2));
}
}
Output
x
p
package main
import "fmt"
/*
Go Program for
Return most occurring character in an input string
*/
func mostOccurrence(text string) byte {
var n int = len(text)
var ans byte = ' '
var max int = 0
// Use to count frequency of characters
// Set initial 0 frequency of possible characters
var count = make([] int,256)
// Counting the value of character element frequency
for i := 0 ; i < n ; i++ {
// Increase frequency
count[text[i]]++
if count[text[i]] > max {
// Get resultant character
ans = text[i]
// Get the max occurring element frequency
max = count[text[i]]
}
}
return ans
}
func main() {
var input1 string = "uxyzzxabxb"
var input2 string = "apple"
// Test
fmt.Printf("%c\n",mostOccurrence(input1))
fmt.Printf("%c\n",mostOccurrence(input2))
}
Output
x
p
<?php
/*
Php Program for
Return most occurring character in an input string
*/
class Occurrence
{
public function mostOccurrence($text)
{
$n = strlen($text);
$ans = ' ';
$max = 0;
// Use to count frequency of characters
// Set initial 0 frequency of possible characters
$count = array_fill(0, 256, 0);
// Counting the value of character element frequency
for ($i = 0; $i < $n; ++$i)
{
// Increase frequency
$count[ord($text[$i])]++;
if ($count[ord($text[$i])] > $max)
{
// Get resultant character
$ans = $text[$i];
// Get the max occurring element frequency
$max = $count[ord($text[$i])];
}
}
return $ans;
}
}
function main()
{
$task = new Occurrence();
$input1 = "uxyzzxabxb";
$input2 = "apple";
// Test
echo($task->mostOccurrence($input1).
"\n");
echo($task->mostOccurrence($input2).
"\n");
}
main();
Output
x
p
/*
Node JS Program for
Return most occurring character in an input string
*/
class Occurrence
{
mostOccurrence(text)
{
var n = text.length;
var ans = ' ';
var max = 0;
// Use to count frequency of characters
// Set initial 0 frequency of possible characters
var count = Array(256).fill(0);
// Counting the value of character element frequency
for (var i = 0; i < n; ++i)
{
// Increase frequency
count[text.charCodeAt(i)]++;
if (count[text.charCodeAt(i)] > max)
{
// Get resultant character
ans = text.charAt(i);
// Get the max occurring element frequency
max = count[text.charCodeAt(i)];
}
}
return ans;
}
}
function main()
{
var task = new Occurrence();
var input1 = "uxyzzxabxb";
var input2 = "apple";
// Test
console.log(task.mostOccurrence(input1));
console.log(task.mostOccurrence(input2));
}
main();
Output
x
p
# Python 3 Program for
# Return most occurring character in an input string
class Occurrence :
def mostOccurrence(self, text) :
n = len(text)
ans = ' '
max = 0
# Use to count frequency of characters
# Set initial 0 frequency of possible characters
count = [0] * (256)
c = ord('a') + 10
i = 0
# Counting the value of character element frequency
while (i < n) :
# Increase frequency
count[ord(text[i])] += 1
if (count[ord(text[i])] > max) :
# Get resultant character
ans = text[i]
# Get the max occurring element frequency
max = count[ord(text[i])]
i += 1
return ans
def main() :
task = Occurrence()
input1 = "uxyzzxabxb"
input2 = "apple"
# Test
print(task.mostOccurrence(input1))
print(task.mostOccurrence(input2))
if __name__ == "__main__": main()
Output
x
p
# Ruby Program for
# Return most occurring character in an input string
class Occurrence
def mostOccurrence(text)
n = text.length
ans = ' '
max = 0
# Use to count frequency of characters
# Set initial 0 frequency of possible characters
count = Array.new(256) {0}
i = 0
# Counting the value of character element frequency
while (i < n)
# Increase frequency
count[text[i].ord] += 1
if (count[text[i].ord] > max)
# Get resultant character
ans = text[i]
# Get the max occurring element frequency
max = count[text[i].ord]
end
i += 1
end
return ans
end
end
def main()
task = Occurrence.new()
input1 = "uxyzzxabxb"
input2 = "apple"
# Test
print(task.mostOccurrence(input1), "\n")
print(task.mostOccurrence(input2), "\n")
end
main()
Output
x
p
import scala.collection.mutable._;
/*
Scala Program for
Return most occurring character in an input string
*/
class Occurrence()
{
def mostOccurrence(text: String): Char = {
var n: Int = text.length();
var ans: Char = ' ';
var max: Int = 0;
// Use to count frequency of characters
// Set initial 0 frequency of possible characters
var count: Array[Int] = Array.fill[Int](256)(0);
var i: Int = 0;
// Counting the value of character element frequency
while (i < n)
{
// Increase frequency
count(text.charAt(i)) += 1;
if (count(text.charAt(i)) > max)
{
// Get resultant character
ans = text.charAt(i);
// Get the max occurring element frequency
max = count(text.charAt(i));
}
i += 1;
}
return ans;
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Occurrence = new Occurrence();
var input1: String = "uxyzzxabxb";
var input2: String = "apple";
// Test
println(task.mostOccurrence(input1));
println(task.mostOccurrence(input2));
}
}
Output
x
p
import Foundation;
/*
Swift 4 Program for
Return most occurring character in an input string
*/
class Occurrence
{
func mostOccurrence(_ data: String) -> Character
{
let n: Int = data.count;
var ans: Character = " ";
var max: Int = 0;
// Use to count frequency of characters
// Set initial 0 frequency of possible characters
var count: [Int] = Array(repeating: 0, count: 256);
var i: Int = 0;
let text = Array(data);
// Counting the value of character element frequency
while (i < n)
{
// Increase frequency
count[Int(UnicodeScalar(String(text[i]))!.value)] += 1;
if (count[Int(UnicodeScalar(String(text[i]))!.value)] > max)
{
// Get resultant character
ans = text[i];
// Get the max occurring element frequency
max = count[Int(UnicodeScalar(String(text[i]))!.value)];
}
i += 1;
}
return ans;
}
}
func main()
{
let task: Occurrence = Occurrence();
let input1: String = "uxyzzxabxb";
let input2: String = "apple";
// Test
print(task.mostOccurrence(input1));
print(task.mostOccurrence(input2));
}
main();
Output
x
p
/*
Kotlin Program for
Return most occurring character in an input string
*/
class Occurrence
{
fun mostOccurrence(text: String): Char
{
val n: Int = text.length;
var ans: Char = ' ';
var max: Int = 0;
// Use to count frequency of characters
// Set initial 0 frequency of possible characters
val count: Array < Int > = Array(256)
{
0
};
var i: Int = 0;
// Counting the value of character element frequency
while (i < n)
{
// Increase frequency
count[text.get(i).toInt()] += 1;
if (count[text.get(i).toInt()] > max)
{
// Get resultant character
ans = text.get(i);
// Get the max occurring element frequency
max = count[text.get(i).toInt()];
}
i += 1;
}
return ans;
}
}
fun main(args: Array < String > ): Unit
{
val task: Occurrence = Occurrence();
val input1: String = "uxyzzxabxb";
val input2: String = "apple";
// Test
println(task.mostOccurrence(input1));
println(task.mostOccurrence(input2));
}
Output
x
p
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment