fork download
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4.  
  5. public class Main {
  6.  
  7. public static void main(String[] args) throws Exception {
  8. final int[][] dataTable = new int[][] {
  9. new int[] {0, 1, 2, 1},
  10. new int[] {0, 1, 3, 1},
  11. new int[] {0, 1, 2, 2},
  12. new int[] {0, 1, 2, 0}
  13. };
  14.  
  15. final Map<Integer, Integer> map = new HashMap<Integer, Integer> ();
  16. for (int i = 0; i < 4; i++) {
  17. for (int j = 0; j < 4; j++) {
  18. final int value = dataTable[i][j];
  19. final Integer currentCount = map.get(value);
  20. final Integer newCount;
  21. if (currentCount == null) {
  22. newCount = 1;
  23. }
  24. else {
  25. newCount = currentCount + 1;
  26. }
  27.  
  28. map.put (value, newCount);
  29. }
  30. }
  31.  
  32. for (final Map.Entry<Integer, Integer> entry : map.entrySet()) {
  33. System.out.println(String.format ("The number %d repeats %d times", entry.getKey(), entry.getValue()));
  34. }
  35. }
  36. }
  37.  
Success #stdin #stdout 0.07s 215552KB
stdin
Standard input is empty
stdout
The number 0 repeats 5 times
The number 1 repeats 6 times
The number 2 repeats 4 times
The number 3 repeats 1 times