In the program, you need to declare a two-dimensional array and find a row or column in which some element is repeated as often as possible.

Example:

0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 0 

Answer: 3rd line.

How could this algorithm be described? I can not think of a skeleton for the code.

For example, I started writing code:

 public static void main(String[] args) { int SIZE = 3, a = 0; int[][] graph = new int[SIZE][SIZE]; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { graph[i][j] = i+1; i = j+1; } } for (int i = 0; i < graph.length; i++) { for (int j = 0; j < graph[i].length; j++) { System.out.print(graph[i][j] +"\t"); } System.out.println(); } for (int i = 0; i < graph.length; i++) { for (int j = 0; j < graph[i].length; j++) { if(graph[i][j] == graph[i+1][j+1]){ a = a + 1; } } } 

    1 answer 1

    Pretty clumsy version made on the knee

      int resultIndex = -1; int maxCountInRow = 0; Map<Integer, Integer> iterationMap; for (int i = 0; i < graph.length; i++) { iterationMap = new HashMap<>(); for (int j = 0; j < graph[i].length; j++) { int value = graph[i][j]; if (iterationMap.containsKey(value)) { iterationMap.put(value, iterationMap.get(value) + 1); } else { iterationMap.put(value, 1); } } for (Map.Entry<Integer, Integer> pair : iterationMap.entrySet()) { if (pair.getValue() > maxCountInRow) { maxCountInRow = pair.getValue(); resultIndex = i; } } } System.out.println(resultIndex); 

    In each line, we count the number of occurrences of each unique element, and at the end we compare it with the results of past iterations.

    • And you can ask step by step? Some nuances that are not clear just ( - Arslan K
    • for Java 8, you can use iterationMap.merge(graph[i][j], 1, Integer::sum) instead of 6 lines of code - Mikhail Ionkin