Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class MostOftenNumerInMatrix {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("Print the number of rows and columns:");
- int numberOfRows = sc.nextInt();
- int numberOfColumns = sc.nextInt();
- int[][] matrix = new int[numberOfRows][numberOfColumns];
- for (int row = 0; row < numberOfRows; row++) {
- for (int col = 0; col < numberOfColumns; col++) {
- System.out.println("Enter value for " + row + " " + col);
- matrix[row][col] = sc.nextInt();
- }
- }
- int count = 0;
- int maxCount = 0;
- int maxIndex = 0;
- int maxNumber = Integer.MIN_VALUE;
- for (int row = 0; row < numberOfRows; row++) {
- for (int col = 0; col < numberOfColumns; col++) {
- int currElement = matrix[row][col];
- if (currElement > maxNumber) {
- maxNumber = currElement;
- }
- }
- }
- int[] mask = new int[maxNumber + 1];
- for (int row = 0; row < numberOfRows; row++) {
- for (int col = 0; col < numberOfColumns; col++) {
- int currElement = matrix[row][col];
- count = ++mask[currElement];
- if (count > maxCount) {
- maxCount = count;
- maxIndex = currElement;
- }
- }
- }
- System.out.println("Chisloto " + maxIndex + " se sresha " + maxCount + " pati");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement