# Java Program to Find Unique Elements in a Matrix

In the previous article, we have discussed Java Program to Find Smallest Element in Each Column of a Matrix

In this article we are going to see how we can write a program to find out the unique elements in the matrix in JAVA language.

## Java Program to Find Unique Elements in 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.

Unique elements are elements that occur only once and doesn’t reappear in the matrix.

For example in the below matrix

2 4 6
6 9 0
4 2 7

Unix Elements are 9, 0, 7 Because other elements are repeated.

Let’s see different ways to find unique elements in a Matrix.

### Method-1: Java Program to Find Unique Elements in a Matrix By Static Initialization of Array Elements

Approach:

• Initialize and declare one 3×3 array with elements.
• Use two for loops to iterate the rows and columns .
• Store the maximum.
• Create a 1D array of maximum size.
• Print all elements where the value of the 1D array is 1(i.e. frequency of the element)

Program:

public class matrix
{
public static void main(String args[])
{
// Initializing the 3X3 matrix i.e. 2D array
int arr[][] = {{10,15,1},{30,50,7},{1,0,0}};
int row, col;

System.out.print("The matrix elements are : ");
printMatrix(arr);
System.out.println("The unique elements in the matrix are : ");
printUnique(arr);

}

// Method to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
System.out.print("\n");
}

// prints unique elements
static void printUnique(int arr[][])
{
int max =0,flag =0,row,col;
for(row=0;row<3;row++)
for(col=0;col<3;col++)
if(max<arr[row][col])
max = arr[row][col];
// Stores the unique element
int temp[]=new int[max+1];
for(row=0;row<3;row++)
for(col=0;col<3;col++)
temp[arr[row][col]]++;
//Prints the unique element
for(row=1;row<=max;row++)
if(temp[row]==1)
{
System.out.print(row + " ");
flag = 1;
}
if(flag==0)
System.out.println("No unique elements");
}

}


Output:

The matrix elements are :
10 15 1
30 50 7
1  0 0
The unique elements in the matrix are :
7 10 15 30 50

### Method-2: Java Program to Find Unique Elements in a Matrix By Dynamic Initialization of Array Elements

Approach:

• Initialize an array of 3×3.
• Ask the user for input and store them in the one array using two for loops.
• Use two for loops to iterate the rows and columns .
• Store the maximum.
• Create a 1D array of maximum size.
• Print all elements where the value of the 1D array is 1(i.e. frequency of the element)

Program:

import java.util.Scanner;
public class matrix{
public static void main(String args[])
{
//Scanner class to take input
Scanner scan = new Scanner(System.in);

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

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

System.out.print("The matrix elements are : ");
printMatrix(arr);
System.out.println("The unique elements in the matrix are : ");
printUnique(arr);

}

// Function to print the matrix
static void printMatrix(int arr[][])
{
int row, col;
// Loop to print the elements
for(row=0;row<3;row++)
{
// Used for formatting
System.out.print("\n");
for(col=0;col<3;col++)
{
System.out.print(arr[row][col]+" ");
}
}
System.out.print("\n");
}

// prints unique elements
static void printUnique(int arr[][])
{
int max =0,flag =0,row,col;
for(row=0;row<3;row++)
for(col=0;col<3;col++)
if(max<arr[row][col])
max = arr[row][col];
// Stores the unique element
int temp[]=new int[max+1];
for(row=0;row<3;row++)
for(col=0;col<3;col++)
temp[arr[row][col]]++;
//Prints the unique element
for(row=1;row<=max;row++)
if(temp[row]==1)
{
System.out.print(row + " ");
flag = 1;
}
if(flag==0)
System.out.println("No unique elements");
}

}


Output:

Enter matrix elements : 1 2 3 1 4 5 6 2 7 8
The matrix elements are :
1 2 3
1 4 5
6 2 7
The unique elements in the matrix are :
3 4 5 6 7

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: