fork(1) 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) throws java.lang.Exception
  11. {
  12. int[][] ia = new int[3][3];
  13. ia[0][0] = 1;
  14. ia[0][1] = 1;
  15. ia[0][2] = 1;
  16. ia[1][0] = 4;
  17. ia[1][1] = 4;
  18. ia[1][2] = 2;
  19. ia[2][0] = 5;
  20. ia[2][1] = 6;
  21. ia[2][2] = 7;
  22. findRepeats(ia, 9);
  23. }
  24.  
  25. public static void findRepeats(int [][] num, int size)
  26. {
  27. int findNum;
  28. int total = 1, row = 0, col = 0;
  29. int [] check = new int[size];
  30. while(row < num.length && col < num[0].length)
  31. {
  32. //Set to number
  33. findNum = num[row][col];
  34. //Cycle array to set next number
  35. if(col < num[0].length-1)
  36. col++;
  37. else
  38. {
  39. row++; //Go to next row if no more columns
  40. col = 0; //Reset column number
  41. }
  42. //Loop through whole array to find repeats
  43. for(int i = row; i < num.length; i++)
  44. {
  45. for(int j = col; j < num[i].length; j++)
  46. {
  47. if(num[i][j] == findNum) {
  48. total++;
  49. //Cycle array to set next number
  50. if(col < num[0].length-1)
  51. col++;
  52. else
  53. {
  54. row++; //Go to next row if no more columns
  55. col = 0; //Reset column number
  56. }
  57. if(row < num.length - 1 && col < num[0].length -1)
  58. num[i][j] = num[row][col];
  59. }
  60. }
  61. }
  62.  
  63.  
  64. //Display total repeats
  65. System.out.println("Number " + findNum + " appears " + total + " times.");
  66. total = 1;
  67. }
  68. }
  69. }
Success #stdin #stdout 0.1s 320256KB
stdin
Standard input is empty
stdout
Number 1 appears 3 times.
Number 4 appears 2 times.
Number 2 appears 1 times.
Number 5 appears 1 times.
Number 6 appears 1 times.
Number 7 appears 1 times.