# Java Program to Print Common Elements in all Rows of a Matrix

In the previous article, we have seen Java Program to Check Diagonally Dominant Matrix

In this article we are going to see how we can write a program to find common element in all row of a given matrix.

## Java Program to Print Common Elements in all Rows of a Matrix

A 3*3 Matrix is having 3 rows and 3 columns where this 3*3 represents the dimension of the matrix. Means there are 3*3 i.e. total 9 elements in a 3*3 Matrix.

Let’s understand it in more simpler way.

                   | A00   A01   A02 |
Matrix A =  | A10   A11   A12 |
| A20   A21   A22 | 3*3
• Matrix A represents a 3*3 matrix.
• A‘ represents the matrix element
• Aij‘ represents the matrix element at it’s matrix position/index.
• i‘ represents the row index
• j‘ represents the column index
• Means A00=Aij  where i=0 and j=0,  A01=aij where i=0 and j=1 and like this.
• Here we have started row value from 0 and column value from 0.

Let’s see different ways to common element in all row  of a given matrix.

### Method-1: Java Program to Print Common Elements in all Rows of a Matrix By Static Initialization of Array Elements

Approach :

• Declare and Initialize a matrix.
• Define ele with elements as keys and their count as values.
• Insert all elements of first row into ele with 1 as their values.
• For remaining rows, check presence of each element in ele.
• If element is present in ele and its count is equal to ‘x’, then increment its count by 1.
• Print ele having common elements.

Program :

import java.util.*;
import java.util.Map.Entry;

public class Main
{
public static void main(String args[])
{
int mat[][] = { { 1, 2, 3 },{ 4, 3, 1 },{ 1, 0, 3 } };

//Define ele with elements as keys and their count as values
HashMap<Integer, Integer> ele = new HashMap<>();

//Insert all elements of first row into ele with 1 as their values
for (int y = 0; y < 3; y++)
ele.put(mat[y], 1);

//For remaining rows, check presence of each element in ele
for (int x = 1; x < 3; x++)
for (int y = 0; y < 3 ; y++)
//If element is present in ele and it's count is equal to 'x',
//then increment its count by 1
if(ele.containsKey(mat[x][y]) && ele.get(mat[x][y]) == x)
ele.put(mat[x][y], x+1);

//Printing ele having common elements
Set<Entry<Integer, Integer>> en = ele.entrySet();
System.out.println("Common Elements In All Rows : ");
for (Entry<Integer, Integer> e : en)
if (e.getValue() == 3)
System.out.print(e.getKey() + " ");
}
}

Output:

Common Elements In All Rows :
1 3

### Method-2: Java Program to Print Common Elements in all Rows of a Matrix By Dynamic Initialization of Array Elements

Approach:

• Take user input of a matrix.
• Define ele with elements as keys and their count as values.
• Insert all elements of first row into ele with 1 as their values.
• For remaining rows, check presence of each element in ele.
• If element is present in ele and its count is equal to ‘x’, then increment its count by 1.
• Print ele having common elements.

Program:

import java.util.*;
import java.util.Map.Entry;
public class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

// Initializing the 3X3 matrix i.e. 2D array
int mat[][] = new int;
int row, col ;

// Taking matrix input
System.out.println("\nEnter matrix elements");
for(row=0;row<3;row++)
for(col=0;col<3;col++)
mat[row][col] = sc.nextInt();

//Define ele with elements as keys and their count as values
HashMap<Integer, Integer> ele = new HashMap<>();

//Insert all elements of first row into ele with 1 as their values
for (int y = 0; y < 3; y++)
ele.put(mat[y], 1);

//For remaining rows, check presence of each element in ele
for (int x = 1; x < 3; x++)
for (int y = 0; y < 3 ; y++)
//If element is present in ele and it's count is equal to 'x',
//then increment its count by 1
if(ele.containsKey(mat[x][y]) && ele.get(mat[x][y]) == x)
ele.put(mat[x][y], x+1);

//Printing ele having common elements
Set<Entry<Integer, Integer>> en = ele.entrySet();
System.out.println("Common Elements In All Rows : ");
for (Entry<Integer, Integer> e : en)
if (e.getValue() == 3)
System.out.print(e.getKey() + " ");
}
}


Output:

Enter matrix elements
Common Elements In All Rows :
1 3

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Related Java Programs: