Given string str. The task is to find the maximum occurring character in the string str.
Examples:
Input: geeksforgeeks
Output: e
Explanation: ‘e’ occurs 4 times in the string
Input: test
Output: t
Explanation: ‘t’ occurs 2 times in the string
Return the maximum occurring character in an input string using Hashing:
Naive approach : ( using unordered_map )
In this approach we simply use the unordered_map from STL to store the frequency of every character and while adding characters to map we take a variable count to determine the element having highest frequency.
Implementation :
C++
#include <bits/stdc++.h>
using namespace std;
char getMaxOccurringChar(string str)
{
unordered_map< char , int >mp;
int n = str.length();
char ans;
int cnt=0;
for ( int i=0 ;i<n ; i++){
mp[str[i]]++;
if (cnt < mp[str[i]]){
ans = str[i];
cnt = mp[str[i]];
}
}
return ans;
}
int main()
{
string str = "sample string" ;
cout << "Max occurring character is: "
<< getMaxOccurringChar(str);
}
|
Java
import java.util.*;
public class Main {
static char getMaxOccurringChar(String str) {
HashMap<Character, Integer> mp = new HashMap<>();
int n = str.length();
char ans = 0 ;
int cnt = 0 ;
for ( int i = 0 ; i < n; i++) {
char c = str.charAt(i);
mp.put(c, mp.getOrDefault(c, 0 ) + 1 );
if (cnt < mp.get(c)) {
ans = c;
cnt = mp.get(c);
}
}
return ans;
}
public static void main(String[] args) {
String str = "sample string" ;
System.out.println( "Max occurring character is: " + getMaxOccurringChar(str));
}
}
|
Python3
def getMaxOccurringChar( str ):
mp = {}
n = len ( str )
ans = ''
cnt = 0
for i in range (n):
if str [i] in mp:
mp[ str [i]] + = 1
else :
mp[ str [i]] = 1
if cnt < mp[ str [i]]:
ans = str [i]
cnt = mp[ str [i]]
return ans
str = "sample string"
print ( "Max occurring character is:" , getMaxOccurringChar( str ))
|
C#
using System;
using System.Collections.Generic;
class MainClass {
public static void Main( string [] args)
{
string str = "sample string" ;
Console.WriteLine( "Max occurring character is: "
+ getMaxOccurringChar(str));
}
static char getMaxOccurringChar( string str)
{
Dictionary< char , int > mp
= new Dictionary< char , int >();
int n = str.Length;
char ans = '\0' ;
int cnt = 0;
for ( int i = 0; i < n; i++) {
if (mp.ContainsKey(str[i])) {
mp[str[i]]++;
}
else {
mp.Add(str[i], 1);
}
if (cnt < mp[str[i]]) {
ans = str[i];
cnt = mp[str[i]];
}
}
return ans;
}
}
|
Javascript
function getMaxOccurringChar(str)
{
let mp = new Map();
let n = str.length;
let ans;
let cnt=0;
for (let i=0 ;i<n ; i++){
mp.set(str[i], (mp.get(str[i]) || 0) + 1);
if (cnt < mp.get(str[i])){
ans = str[i];
cnt = mp.get(str[i]);
}
}
return ans;
}
let str = "sample string" ;
console.log( "Max occurring character is: " + getMaxOccurringChar(str));
|
C
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char getMaxOccurringChar( char str[])
{
int count[256] = { 0 };
int length = strlen (str);
for ( int i = 0; i < length; i++)
count[( int )str[i]]++;
char maxChar;
int maxCount = 0;
for ( int i = 0; i < length; i++) {
if (count[( int )str[i]] > maxCount) {
maxCount = count[( int )str[i]];
maxChar = str[i];
}
}
return maxChar;
}
int main()
{
char str[] = "sample string" ;
printf ( "Max occurring character is: %c\n" ,
getMaxOccurringChar(str));
return 0;
}
|
OutputMax occurring character is: s
Time Complexity: O(N), Traversing the string of length N one time.
Auxiliary Space: O(N), where N is the size of the string
The idea is to store the frequency of every character in the array and return the character with maximum count.
Follow the steps to solve the problem:
- Create a count array of size 256 to store the frequency of every character of the string
- Maintain a max variable to store the maximum frequency so far whenever encounter a frequency more than the max then update the max
- And update that character in our result variable.
Below is the implementation of the above approach:
C
#include <stdio.h>
#include <string.h>
#define ASCII_SIZE 256
char getMaxOccurringChar( char * str)
{
int count[ASCII_SIZE] = { 0 };
int len = strlen (str);
int max = 0;
char result;
for ( int i = 0; i < len; i++) {
count[str[i]]++;
if (max < count[str[i]]) {
max = count[str[i]];
result = str[i];
}
}
return result;
}
int main()
{
char str[] = "sample string" ;
printf ( "Max occurring character is %c" ,
getMaxOccurringChar(str));
}
|
C++
#include <bits/stdc++.h>
#define ASCII_SIZE 256
using namespace std;
char getMaxOccurringChar( char * str)
{
int count[ASCII_SIZE] = { 0 };
int len = strlen (str);
int max = 0;
char result;
for ( int i = 0; i < len; i++) {
count[str[i]]++;
if (max < count[str[i]]) {
max = count[str[i]];
result = str[i];
}
}
return result;
}
int main()
{
char str[] = "sample string" ;
cout << "Max occurring character is "
<< getMaxOccurringChar(str);
}
|
Java
public class GFG {
static final int ASCII_SIZE = 256 ;
static char getMaxOccurringChar(String str)
{
int count[] = new int [ASCII_SIZE];
int len = str.length();
for ( int i = 0 ; i < len; i++)
count[str.charAt(i)]++;
int max = - 1 ;
char result = ' ' ;
for ( int i = 0 ; i < len; i++) {
if (max < count[str.charAt(i)]) {
max = count[str.charAt(i)];
result = str.charAt(i);
}
}
return result;
}
public static void main(String[] args)
{
String str = "sample string" ;
System.out.println( "Max occurring character is "
+ getMaxOccurringChar(str));
}
}
|
Python3
ASCII_SIZE = 256
def getMaxOccurringChar( str ):
count = [ 0 ] * ASCII_SIZE
max = - 1
c = ''
for i in str :
count[ ord (i)] + = 1
for i in str :
if max < count[ ord (i)]:
max = count[ ord (i)]
c = i
return c
str = "sample string"
print ( "Max occurring character is" , getMaxOccurringChar( str ))
|
C#
using System;
class GFG {
static int ASCII_SIZE = 256;
static char getMaxOccurringChar(String str)
{
int [] count = new int [ASCII_SIZE];
int len = str.Length;
for ( int i = 0; i < len; i++)
count[str[i]]++;
int max = -1;
char result = ' ' ;
for ( int i = 0; i < len; i++) {
if (max < count[str[i]]) {
max = count[str[i]];
result = str[i];
}
}
return result;
}
public static void Main()
{
String str = "sample string" ;
Console.Write( "Max occurring character is "
+ getMaxOccurringChar(str));
}
}
|
PHP
<?php
$ASCII_SIZE = 256;
function getMaxOccurringChar( $str )
{
global $ASCII_SIZE ;
$count = array_fill (0, $ASCII_SIZE , NULL);
$len = strlen ( $str );
$max = 0;
for ( $i = 0; $i < ( $len ); $i ++)
{
$count [ord( $str [ $i ])]++;
if ( $max < $count [ord( $str [ $i ])])
{
$max = $count [ord( $str [ $i ])];
$result = $str [ $i ];
}
}
return $result ;
}
$str = "sample string" ;
echo "Max occurring character is " .
getMaxOccurringChar( $str );
?>
|
Javascript
<script>
let ASCII_SIZE = 256;
function getMaxOccurringChar(str)
{
let count = new Array(ASCII_SIZE);
for (let i = 0; i < ASCII_SIZE; i++)
{
count[i] = 0;
}
let len = str.length;
for (let i = 0; i < len; i++)
{
count[str[i].charCodeAt(0)] += 1;
}
let max = -1;
let result = ' ' ;
for (let i = 0; i < len; i++)
{
if (max < count[str[i].charCodeAt(0)])
{
max = count[str[i].charCodeAt(0)];
result = str[i];
}
}
return result;
}
let str = "sample string" ;
document.write( "Max occurring character is " , getMaxOccurringChar(str));
</script>
|
OutputMax occurring character is s
Time Complexity: O(N), Traversing the string of length N one time.
Auxiliary Space: O(1)