Guest User

Most Frequent Character

a guest
Dec 6th, 2020
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. public class Main{
  2. static final int ASCII_SIZE = 256;
  3. static char getMaxOccuringChar(String str2){
  4. int count[] = new int[ASCII_SIZE];
  5.  
  6. int len = str2.length();
  7. for (int i = 0; i < len; i++)
  8. count[str2.charAt(i)]++;
  9. int max = -1;
  10. char result = ' ';
  11.  
  12. for (int i = 0; i < len; i++) {
  13. if (max < count[str2.charAt(i)]) {
  14. max = count[str2.charAt(i)];
  15. result = str2.charAt(i);
  16. }
  17. }
  18.  
  19. return result;
  20. }
  21.  
  22. public static void main(String[] args){
  23. String str = "IN A RIGHT TRIANGLE THE SQUARE OF THE HYPOTENUSE EQUALS THE SUM OF THE SQUARES OF THE LEGS";
  24. String str2 = str.toLowerCase();
  25. System.out.println("Max occurring character is: " +
  26. getMaxOccuringChar(str2));
  27. }
  28. }
  29.  
  30.  
  31.  
  32.  
  33.  
Advertisement
Add Comment
Please, Sign In to add comment