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 = "Sscaler by interviewbit";
  8. // counter array to store frequency of each character.
  9. int frequency[] = new int [256];
  10.  
  11. // iterate through the string
  12. for (int i = 0; i < str.length(); i++) {
  13. // increase count by 1 in the array
  14. // at index based on the character
  15. frequency[(int) str.charAt(i)]++;
  16. }
  17.  
  18. // traverse through the counter array
  19. for (int i = 0; i < frequency.length; i++) {
  20. // if frequency of the character is not 0
  21. if (frequency[i] != 0) {
  22. // print the character along with its frequency
  23. System.out.println((char) i + " - " + frequency[i]);
  24. }
  25. }
  26. }
  27. }
  28.  
Success #stdin #stdout 0.14s 55476KB
stdin
Standard input is empty
stdout
  - 2
S - 1
a - 1
b - 2
c - 1
e - 3
i - 3
l - 1
n - 1
r - 2
s - 1
t - 2
v - 1
w - 1
y - 1