Created
December 9, 2017 10:41
-
-
Save harshityadav95/54ed6154ac717e9c934d494ce26dd408 to your computer and use it in GitHub Desktop.
Character having Highest Frequencey in string "aaabbabsssssssssssss" will give output "s"
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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