Skip to content

Instantly share code, notes, and snippets.

@harshityadav95
Created December 9, 2017 10:41
Show Gist options
  • Save harshityadav95/54ed6154ac717e9c934d494ce26dd408 to your computer and use it in GitHub Desktop.
Save harshityadav95/54ed6154ac717e9c934d494ce26dd408 to your computer and use it in GitHub Desktop.
Character having Highest Frequencey in string "aaabbabsssssssssssss" will give output "s"
static String findHighestFreqChar(String inputStr) {
// Create array to keep the count of individual
// characters and initialize the array as 0
int count[] = new int[256];
// Construct character count array from the input
// string.
int len = inputStr.length();
for (int i=0; i<len; i++)
count[inputStr.charAt(i)]++;
int max = -1; // Initialize max count
String result=""; // Initialize result
// Traversing through the string and maintaining
// the count of each character
for (int i = 0; i < len; i++) {
if (max < count[inputStr.charAt(i)]) {
max = count[inputStr.charAt(i)];
result = String.valueOf(inputStr.charAt(i));
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment