Print the most occurring character in an array of strings

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:



  1. Each string in the array is traversed.
  2. For every character in the string, its count is incremented by 1 in the hash.
  3. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to print the most occurring
// character in an array of strings
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the most occurring character
void findMostOccurringChar(vector<string> str)
{
  
    // Creating a hash of size 26
    int hash[26] = { 0 };
  
    // For loop to iterate through
    // every string of the array
    for (int i = 0; i < str.size(); i++) {
  
        // For loop to iterate through
        // every character of the string
        for (int j = 0; j < str[i].length(); j++) {
  
            // Incrementing the count of
            // the character in the hash
            hash[str[i][j]]++;
        }
    }
  
    // Finding the character
    // with the maximum count
    int max = 0;
    for (int i = 0; i < 26; i++) {
        max = hash[i] > hash[max] ? i : max;
    }
  
    cout << (char)(max + 97) << endl;
}
  
// Driver code
int main()
{
  
    // Declaring Vector of String type
    vector<string> str;
    str.push_back("animal");
    str.push_back("zebra");
    str.push_back("lion");
    str.push_back("giraffe");
  
    findMostOccurringChar(str);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to print the most occurring
// character in an array of Strings
import java.util.*;
  
class GFG
{
  
// Function to print the most occurring character
static void findMostOccurringChar(Vector<String> str)
{
  
    // Creating a hash of size 26
    int []hash = new int[26];
  
    // For loop to iterate through
    // every String of the array
    for (int i = 0; i < str.size(); i++)
    {
  
        // For loop to iterate through
        // every character of the String
        for (int j = 0; j < str.get(i).length(); j++) 
        {
  
            // Incrementing the count of
            // the character in the hash
            hash[str.get(i).charAt(j)-97]++;
        }
    }
  
    // Finding the character
    // with the maximum count
    int max = 0;
    for (int i = 0; i < 26; i++) 
    {
        max = hash[i] > hash[max] ? i : max;
    }
  
    System.out.print((char)(max + 97) +"\n");
}
  
// Driver code
public static void main(String[] args)
{
  
    // Declaring Vector of String type
    Vector<String> str = new Vector<String>();
    str.add("animal");
    str.add("zebra");
    str.add("lion");
    str.add("giraffe");
  
    findMostOccurringChar(str);
}
}
  
// This code is contributed by PrinciRaj1992

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print the most occurring 
# character in an array of strings 
  
# Function to print the most occurring character 
def findMostOccurringChar(string) :
  
    # Creating a hash of size 26 
    hash = [0]*26
  
    # For loop to iterate through 
    # every string of the array 
    for i in range(len(string)) :
  
        # For loop to iterate through 
        # every character of the string 
        for j in range(len(string[i])) :
  
            # Incrementing the count of 
            # the character in the hash 
            hash[ord(string[i][j]) - ord('a')] += 1
  
    # Finding the character 
    # with the maximum count 
    max = 0
    for i in range(26) :
        max = i if hash[i] > hash[max] else max
  
    print((chr)(max + 97)); 
  
# Driver code 
if __name__ == "__main__"
  
    # Declaring Vector of String type 
    string = []; 
    string.append("animal"); 
    string.append("zebra"); 
    string.append("lion"); 
    string.append("giraffe"); 
  
    findMostOccurringChar(string); 
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to print the most occurring 
// character in an array of Strings 
using System;
  
class GFG 
  
    // Function to print the most occurring character 
    static void findMostOccurringChar(string []str) 
    
      
        // Creating a hash of size 26 
        int []hash = new int[26]; 
      
        // For loop to iterate through 
        // every String of the array 
        for (int i = 0; i < str.Length; i++) 
        
      
            // For loop to iterate through 
            // every character of the String 
            for (int j = 0; j < str[i].Length; j++) 
            
      
                // Incrementing the count of 
                // the character in the hash 
                hash[str[i][j]-97]++; 
            
        
      
        // Finding the character 
        // with the maximum count 
        int max = 0; 
        for (int i = 0; i < 26; i++) 
        
            max = hash[i] > hash[max] ? i : max; 
        
      
        Console.Write((char)(max + 97) +"\n"); 
    
      
    // Driver code 
    public static void Main(String[] args) 
    
      
        // Declaring Vector of String type 
        string []str = {"animal","zebra","lion","giraffe"}; 
      
        findMostOccurringChar(str); 
    
  
// This code is contributed by AnkitRai01

chevron_right


Output:

a

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : princiraj1992, AnkitRai01

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.