public class HighestOccurence {
public static void main(String args[]) {
String str = "sushil";
char[] chars = str.toCharArray();
int[] arr = new int[26];// number of char possible
int maxCount = 0;
char c = 0;
for (int i = 0; i < chars.length; i++) {
int index = chars[i] - 'a'; // reduce offset
arr[index] = arr[index] + 1;
if (arr[index] > maxCount) {
maxCount = arr[index];
c = chars[i];
}
}
System.out.println(String.valueOf(c));
}
output: s
public static void main(String args[]) {
String str = "sushil";
char[] chars = str.toCharArray();
int[] arr = new int[26];// number of char possible
int maxCount = 0;
char c = 0;
for (int i = 0; i < chars.length; i++) {
int index = chars[i] - 'a'; // reduce offset
arr[index] = arr[index] + 1;
if (arr[index] > maxCount) {
maxCount = arr[index];
c = chars[i];
}
}
System.out.println(String.valueOf(c));
}
output: s
No comments:
Post a Comment