Given an array arr[] of lowercase strings, the task is to print the most occurring character in the set of strings.
Examples:
Input: arr[] = {“animal”, “zebra”, “lion”, “giraffe”}
Output: a
Explanation:
The frequency of ‘a’ is 4 which is highest.
Input: arr[] = {“aa”, “bb”, “cc”, “bde”}
Output: b
Approach: The idea is to implement a hash data structure using an array of size 26. This array stores the count of each character from ‘a’ to ‘z’. The following steps can be followed to compute the answer:
- Each string in the array is traversed.
- For every character in the string, its count is incremented by 1 in the hash.
- After all, strings are traversed, the maximum count of the character is checked and the character is printed.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
void findMostOccurringChar(vector<string> str)
{
int hash[26] = { 0 };
for ( int i = 0; i < str.size(); i++) {
for ( int j = 0; j < str[i].length(); j++) {
hash[str[i][j]]++;
}
}
int max = 0;
for ( int i = 0; i < 26; i++) {
max = hash[i] > hash[max] ? i : max;
}
cout << ( char )(max + 97) << endl;
}
int main()
{
vector<string> str;
str.push_back( "animal" );
str.push_back( "zebra" );
str.push_back( "lion" );
str.push_back( "giraffe" );
findMostOccurringChar(str);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findMostOccurringChar(Vector<String> str)
{
int []hash = new int [ 26 ];
for ( int i = 0 ; i < str.size(); i++)
{
for ( int j = 0 ; j < str.get(i).length(); j++)
{
hash[str.get(i).charAt(j)- 97 ]++;
}
}
int max = 0 ;
for ( int i = 0 ; i < 26 ; i++)
{
max = hash[i] > hash[max] ? i : max;
}
System.out.print(( char )(max + 97 ) + "\n" );
}
public static void main(String[] args)
{
Vector<String> str = new Vector<String>();
str.add( "animal" );
str.add( "zebra" );
str.add( "lion" );
str.add( "giraffe" );
findMostOccurringChar(str);
}
}
|
Python3
def findMostOccurringChar(string) :
hash = [ 0 ] * 26 ;
for i in range ( len (string)) :
for j in range ( len (string[i])) :
hash [ ord (string[i][j]) - ord ( 'a' )] + = 1 ;
max = 0 ;
for i in range ( 26 ) :
max = i if hash [i] > hash [ max ] else max ;
print (( chr )( max + 97 ));
if __name__ = = "__main__" :
string = [];
string.append( "animal" );
string.append( "zebra" );
string.append( "lion" );
string.append( "giraffe" );
findMostOccurringChar(string);
|
C#
using System;
class GFG
{
static void findMostOccurringChar( string []str)
{
int []hash = new int [26];
for ( int i = 0; i < str.Length; i++)
{
for ( int j = 0; j < str[i].Length; j++)
{
hash[str[i][j]-97]++;
}
}
int max = 0;
for ( int i = 0; i < 26; i++)
{
max = hash[i] > hash[max] ? i : max;
}
Console.Write(( char )(max + 97) + "\n" );
}
public static void Main(String[] args)
{
string []str = { "animal" , "zebra" , "lion" , "giraffe" };
findMostOccurringChar(str);
}
}
|
Javascript
<script>
function findMostOccurringChar(str)
{
var hash = Array(26).fill(0);
for ( var i = 0; i < str.length; i++)
{
for ( var j = 0; j < str[i].length; j++)
{
hash[str[i][j]]++;
}
}
var max = 0;
for ( var i = 0; i < 26; i++) {
max = hash[i] > hash[max] ? i : max;
}
document.write(String.fromCharCode(max + 97));
}
var str = [];
str.push( "animal" );
str.push( "zebra" );
str.push( "lion" );
str.push( "giraffe" );
findMostOccurringChar(str);
</script>
|
Time Complexity: O(n * l), where n is the size of the given vector of strings and l is the maximum length of a string in the given vector.
Auxiliary Space: O(26) ? O(1), no extra space is required, so it is a constant.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
02 Jan, 2023
Like Article
Save Article