Print the most occurring character in an array of strings Last Updated : 02 Jan, 2023 Comments Improve Suggest changes Like Article Like Report 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 // 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; } 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<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 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 JavaScript <script> // JavaScript program to print the most occurring // character in an array of strings // Function to print the most occurring character function findMostOccurringChar(str) { // Creating a hash of size 26 var hash = Array(26).fill(0); // For loop to iterate through // every string of the array for (var i = 0; i < str.length; i++) { // For loop to iterate through // every character of the string for (var 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 var max = 0; for (var i = 0; i < 26; i++) { max = hash[i] > hash[max] ? i : max; } document.write(String.fromCharCode(max + 97)); } // Driver code // Declaring Vector of String type var str = []; str.push("animal"); str.push("zebra"); str.push("lion"); str.push("giraffe"); findMostOccurringChar(str); </script> Output: a 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. Comment More infoAdvertise with us Next Article Print the most occurring character in an array of strings C code_r Follow Improve Article Tags : Strings Hash DSA Arrays Practice Tags : ArraysHashStrings Similar Reads Print all the duplicate characters in a string Given a string s, the task is to identify all characters that appear more than once and print each as a list containing the character and its count. Examples:Input: s = "geeksforgeeks"Output: ['e', 4], ['g', 2], ['k', 2], ['s', 2]Explanation: Characters e, g, k, and s appear more than once. Their co 8 min read Check if max occurring character of one string appears same no. of times in other Given two strings, we need to take the character which has the maximum occurrence in the first string, and then we have to check if that particular character is present in the second string the same number of times as it is present in the first string.Examples: Input : s1 = "sssgeek", s2 = "geeksss" 7 min read Sort an array of strings based on count of distinct characters Given a string array arr[] as input, the task is to print the words sorted by number of distinct characters that occur in the word, followed by length of word. Note: If two words have same number of distinct characters, the word with more total characters comes first. If two words have same number o 6 min read Count substrings with each character occurring at most k times Given a string S. Count number of substrings in which each character occurs at most k times. Assume that the string consists of only lowercase English alphabets. Examples: Input : S = ab k = 1 Output : 3 All the substrings a, b, ab have individual character count less than 1. Input : S = aaabb k = 2 15+ min read Find maximum occurring character in a string Given string str. The task is to find the maximum occurring character in the string str.Examples:Input: geeksforgeeksOutput: eExplanation: 'e' occurs 4 times in the stringInput: testOutput: tExplanation: 't' occurs 2 times in the stringReturn the maximum occurring character in an input string using 8 min read Maximize length of the String by concatenating characters from an Array of Strings Find the largest possible string of distinct characters formed using a combination of given strings. Any given string has to be chosen completely or not to be chosen at all. Examples: Input: strings ="abcd", "efgh", "efgh" Output: 8Explanation: All possible combinations are {"", "abcd", "efgh", "abc 12 min read Print common characters of two Strings in alphabetical order Given two strings, print all the common characters in lexicographical order. If there are no common letters, print -1. All letters are lower case. Examples: Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e(2 times), k(1 6 min read Most frequent word in an array of strings Given an array of words arr[], The task is to find the most occurring word in arr[]. Examples: Input : arr[] = {"geeks", "for", "geeks", "a", "portal", "to", "learn", "can", "be", "computer", "science", "zoom", "yup", "fire", "in", "be", "data", "geeks"}Output : geeks Explanation : "geeks" is the mo 15+ min read Print all distinct characters of a string in order (3 Methods) Given a string, find the all distinct (or non-repeating characters) in it. For example, if the input string is âGeeks for Geeksâ, then output should be 'for' and if input string is âGeeks Quizâ, then output should be âGksQuizâ.The distinct characters should be printed in same order as they appear in 14 min read Sort a string according to the frequency of characters Given a string str, the task is to sort the string according to the frequency of each character, in ascending order. If two elements have the same frequency, then they are sorted in lexicographical order.Examples: Input: str = "geeksforgeeks" Output: forggkksseeee Explanation: Frequency of character 14 min read Like