fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. // declaring a hardcoded int[] arr
  10. int[] arr = new int[]{1, 1, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4};
  11.  
  12. // delcaring a freqency array. Considering we know that max value in arr is N
  13. // which in this case happens to be 4
  14. int[] freqArr = new int[5];
  15.  
  16. for(int i : arr){
  17. freqArr[i] = freqArr[i]+1;
  18. }
  19. // we have constructed our frequency array. now we will print the frequencies of
  20. //all elemnets in our input
  21. for(int i=1; i<5; i++){
  22. System.out.println("Frequency of element "+ i +" is "+ freqArr[i]);
  23. }
  24. }
  25. }
Success #stdin #stdout 0.13s 57444KB
stdin
Standard input is empty
stdout
Frequency of element 1 is 2
Frequency of element 2 is 1
Frequency of element 3 is 4
Frequency of element 4 is 6