-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMaxOccurringCharacter.java
37 lines (34 loc) · 1.11 KB
/
MaxOccurringCharacter.java
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
package Problems_On_String;
import java.util.Arrays;
public class MaxOccurringCharacter {
static char maxOccurringChar(String str) {
char ans = str.charAt(0);
int i, curr_freq = 0, max_freq = 0, n = str.length();
for (i = 1; i < n; i++) {
if (str.charAt(i) == str.charAt(i - 1)) {
curr_freq++;
} else {
if (max_freq < curr_freq) {
max_freq = curr_freq;
ans = str.charAt(i - 1);
}
curr_freq = 0;
}
}
if (max_freq < curr_freq) {
max_freq = curr_freq;
ans = str.charAt(i - 1);
}
return ans;
}
public static void main(String[] args) {
String str = "takeuforward";
// convert to character array
char charArr[] = str.toCharArray();
// sort the character array
Arrays.sort(charArr);
// sorted character array converted back to string
str = new String(charArr);
System.out.println("Maximum Occurring Character is " + maxOccurringChar(str));
}
}