原文:

给定小写字符串数组arr[],任务是打印字符串集中出现次数最多的字符。

示例

输入arr[] = {"animal", "zebra", "lion", "giraffe"}

输出a

说明a的频率是 4,这是最高的。

输入arr[] = {"aa", "bb", "cc", "bde"}

输出b

方法:想法是使用大小为 26 的数组实现。此数组存储从az的每个字符的计数。 可以按照以下步骤计算答案:

  1. 遍历数组中的每个字符串。

  2. 对于字符串中的每个字符,其值在哈希中均增加 1。

  3. 遍历所有字符串之后,将检查字符的最大计数并打印字符。

下面是上述方法的实现:

cpp

// c   program to print the most occurring 
// character in an array of strings 
#include  
using namespace std; 
// function to print the most occurring character 
void findmostoccurringchar(vector 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 str; 
    str.push_back("animal"); 
    str.push_back("zebra"); 
    str.push_back("lion"); 
    str.push_back("giraffe"); 
    findmostoccurringchar(str); 
    return 0; 
} 

java

// 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 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 str = new vector(); 
    str.add("animal"); 
    str.add("zebra"); 
    str.add("lion"); 
    str.add("giraffe"); 
    findmostoccurringchar(str); 
} 
} 
// this code is contributed by princiraj1992 

python3

# 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 

c

// 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 

输出

a