Maximum repeated frequency of characters in a given string
Last Updated :
09 Jun, 2021
Given a string S, the task is to find the count of maximum repeated frequency of characters in the given string S.
Examples:
Input: S = “geeksgeeks”
Output: Frequency 2 is repeated 3 times
Explanation:
Frequency of characters in the given string –
{“g”: 2, “e”: 4, “k”: 2, “s”: 2}
The frequency 2 is repeated thrice for the characters “g”, “k”, “s”.
Input: S = “abcabcdedee”
Output: Frequency 2 is repeated 4 times.
Explanation:
Frequency of characters in the given string –
{“a”: 2, “b”: 2, “c”: 2, “d”: 2, “e”: 3}
The frequency 2 is repeated four times for the characters “a”, “b”, “c”, “d”.
Efficient Approach:
- The idea is to first store the frequency of characters of the string in an array of size 26. Since all characters of strings are among 26 lowercase English alphabets, we can store the frequency of characters in an array of size 26.
- Create a hash map, to store the count of frequencies of the characters and return the frequency which occurred max times.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findMaxFrequency(string s)
{
int arr[26] = { 0 };
for ( int i = 0; i < s.length(); i++)
arr[s[i] - 'a' ]++;
unordered_map< int , int > hash;
for ( int i = 0; i < 26; i++)
if (arr[i] != 0)
hash[arr[i]]++;
int max_count = 0, res = -1;
for ( auto i : hash) {
if (max_count < i.second) {
res = i.first;
max_count = i.second;
}
}
cout << "Frequency " << res << " is repeated "
<< max_count<< " times" ;
}
int main()
{
string s = "geeksgeeks" ;
findMaxFrequency(s);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findMaxFrequency(String s)
{
int arr[] = new int [ 26 ];
for ( int i = 0 ; i < s.length(); i++)
arr[s.charAt(i) - 'a' ]++;
HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();
for ( int i = 0 ; i < 26 ; i++)
if (arr[i] != 0 ) {
if (hash.containsKey(arr[i])){
hash.put(arr[i], hash.get(arr[i])+ 1 );
}
else {
hash.put(arr[i], 1 );
}
}
int max_count = 0 , res = - 1 ;
for (Map.Entry<Integer,Integer> i : hash.entrySet()){
if (max_count < i.getValue()) {
res = i.getKey();
max_count = i.getValue();
}
}
System.out.println( "Frequency " + res+ " is repeated "
+ max_count+ " times" );
}
public static void main(String[] args)
{
String s = "geeksgeeks" ;
findMaxFrequency(s);
}
}
|
Python3
def findMaxFrequency(s):
arr = [ 0 ] * 26
for i in range ( len (s)):
arr[ ord (s[i]) - ord ( 'a' )] + = 1
hash = {}
for i in range ( 26 ):
if (arr[i] ! = 0 ):
if arr[i] not in hash :
hash [arr[i]] = 0
hash [arr[i]] + = 1
max_count = 0
res = - 1
for i in hash :
if (max_count < hash [i]):
res = i
max_count = hash [i]
print ( "Frequency" , res, "is repeated" , max_count, "times" )
s = "geeksgeeks"
findMaxFrequency(s)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void findMaxFrequency(String s)
{
int []arr = new int [26];
for ( int i = 0; i < s.Length; i++)
arr[s[i] - 'a' ]++;
Dictionary< int , int > hash = new Dictionary< int , int >();
for ( int i = 0; i < 26; i++)
if (arr[i] != 0) {
if (hash.ContainsKey(arr[i])){
hash[arr[i]] = hash[arr[i]]+1;
}
else {
hash.Add(arr[i], 1);
}
}
int max_count = 0, res = -1;
foreach ( KeyValuePair< int , int > i in hash){
if (max_count < i.Value) {
res = i.Key;
max_count = i.Value;
}
}
Console.WriteLine( "Frequency " + res+ " is repeated "
+ max_count+ " times" );
}
public static void Main(String[] args)
{
String s = "geeksgeeks" ;
findMaxFrequency(s);
}
}
|
Javascript
<script>
function findMaxFrequency(s)
{
var arr = Array(26).fill(0);
for ( var i = 0; i < s.length; i++)
arr[s[i].charCodeAt(0) - 'a' .charCodeAt(0)]++;
var hash = new Map();
for ( var i = 0; i < 26; i++)
if (arr[i] != 0)
{
if (hash.has(arr[i]))
hash.set(arr[i], hash.get(arr[i])+1)
else
hash.set(arr[i], 1)
}
var max_count = 0, res = -1;
hash.forEach((value, key) => {
if (max_count < value) {
res = key;
max_count = value;
}
});
document.write( "Frequency " + res + " is repeated "
+ max_count+ " times" );
}
var s = "geeksgeeks" ;
findMaxFrequency(s);
</script>
|
Output:
Frequency 2 is repeated 3 times
Performance Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...