fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) {
  11. Random rand = new Random();
  12. int[] array = new int[100];
  13. for (int i = 0; i < array.length; i++) array[i] = rand.nextInt(21);
  14.  
  15. int[] count = count(array, 20);
  16. for (int i = 0; i < count.length; i++)
  17. System.out.println(i + ": " + count[i]);
  18. }
  19.  
  20. public static int[] count(int[] array, int maxValue) {
  21. int[] count = new int[maxValue + 1];
  22. // for each value.
  23. for (int n : array) count[n]++; // increment the counter for that value.
  24. return count;
  25. }
  26. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
0: 5
1: 5
2: 4
3: 6
4: 4
5: 9
6: 5
7: 9
8: 1
9: 3
10: 4
11: 11
12: 2
13: 6
14: 1
15: 6
16: 5
17: 3
18: 4
19: 4
20: 3