-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighest_occuring_character.txt
62 lines (50 loc) · 1.66 KB
/
Highest_occuring_character.txt
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
59
60
61
62
Highest Occuring Character
Send Feedback
For a given a string(str), find and return the highest occurring character.
Example:
Input String: "abcdeapapqarr"
Expected Output: 'a'
Since 'a' has appeared four times in the string which happens to be the highest frequency character, the answer would be 'a'.
If there are two characters in the input string with the same frequency, return the character which comes first.
Consider:
Assume all the characters in the given string to be in lowercase always.
Input Format:
The first and only line of input contains a string without any leading and trailing spaces.
Output Format:
The only line of output prints the updated string.
Note:
You are not required to print anything explicitly. It has already been taken care of.
Constraints:
0 <= N <= 10^6
Where N is the length of the input string.
Time Limit: 1 second
Sample Input 1:
abdefgbabfba
Sample Output 1:
b
Sample Input 2:
xy
Sample Output 2:
x
// highest occurring character in the String.
public class solution {
public static char highestOccuringCharacter(String inputString) {
// Write your code here
int n = inputString.length();
int a[] = new int[256];
for(int i=0; i<n; i++){
char ch = inputString.charAt(i);
a[(int)ch]++;
}
char c = inputString.charAt(0), ans = c;
int max = a[(int)c];
for(int i=1; i<n; i++){
char ch = inputString.charAt(i);
if(a[(int)ch]>max){
max = a[(int)ch];
ans = ch;
}
}
return ans;
}
}