-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maximum Occuring Character.cpp
58 lines (49 loc) · 1.11 KB
/
Maximum Occuring Character.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
//Function to find the maximum occurring character in a string.
char getMaxOccuringChar(string s)
{
int arr[26] = {0};
// create an array of count of charracter
for(int i=0; i<s.size(); i++){
// lowercase
int num = 0;
if(s[i] >= 'a' and s[i] <= 'z'){
num = s[i] - 'a';
}
else{
// uppercase
num = s[i] - 'A';
}
arr[num]++;
}
int maxi = -1;
int ans = 0;
for(int i=0; i<26; i++){
if(maxi < arr[i]){
ans = i;
maxi = arr[i];
}
}
return 'a' + ans;
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin >> t;
while(t--)
{
string str;
cin >> str;
Solution obj;
cout<< obj.getMaxOccuringChar(str)<<endl;
}
}
// } Driver Code Ends