fork(1) download
  1. import java.util.*;
  2.  
  3. class FindFrequencyExample2 {
  4.  
  5. public static void main(String args[]) {
  6. // given string
  7. String str = "ScAaler by interviewbit";
  8. // counter array to store the frequency of each character.
  9. int freq[] = new int[str.length()];
  10.  
  11. // convert the string into character array
  12. // store it in ch[]
  13. char ch[] = str.toCharArray();
  14.  
  15. // traverse through each character in the ch[] array
  16. for(int i=0;i<ch.length-1;i++){
  17. // set freq[i] as 1
  18. // for the first time visiting each new character
  19. freq[i]=1;
  20. // iterate through the rest of the characters
  21. // ScAaler by interviewbit";
  22. for(int j=i+1;j<ch.length;j++)
  23. {
  24. // if more ch[i] characters are present
  25. if(ch[i]==ch[j])
  26. {
  27. // increase the count by 1
  28. freq[i]++;
  29. // set ch[j] to 0 to avoid counting visited characters
  30. ch[j]='0';
  31. }
  32.  
  33. }
  34. }
  35.  
  36. // traverse through the freq array
  37. for (int i = 0; i < freq.length; i++)
  38. {
  39. // if characters in ch[] array is not '0' and ' '
  40. if(ch[i]!='0' && ch[i]!=' ')
  41. {
  42. // print the character along with its frequency
  43. System.out.println(ch[i]+" - "+freq[i]);
  44. }
  45. }
  46.  
  47. }
  48. }
  49.  
Success #stdin #stdout 0.18s 57556KB
stdin
Standard input is empty
stdout
S - 1
c - 1
A - 1
a - 1
l - 1
e - 3
r - 2
b - 2
y - 1
i - 3
n - 1
t - 2
v - 1
w - 1