# HOW TO FIND UNIQUE ELEMENTS IN A MATRIX USING JAVA

In this tutorial, we will learn how to find unique elements in a matrix in Java. Unique elements refer to the elements that are present only once in a matrix or we can say that the element in a matrix whose frequency is one.

## Find the unique element in an array

Example:

```let us take an example of a matrix |1,2,3|
|4,3,6|
|7,8,9|
```

in the above example, it is a 3X3 matrix. In 3X3, 3 and 3 refers to the numbers of rows and columns. We can say that the size of the array is represented by (no.of rows)X(no. of columns). The total number of elements present in the matrix is calculated by finding the product of the number of rows to the number of columns. In this case the number of elements=3*3=9. In the above example, we can say that there is only one element whose frequency is greater than 1. So we can say that 1,2,4,6,7,8,9 are the unique elements. And 3 is a non-unique element as its frequency is 2 (greater than 1).

```package javaapplication27;

public class JavaApplication27 {
static int rows=4;
static int columns=4;

static void uelement(int matrix[][], int n, int m){
int maxvalue=0;
int flag=0;
for (int i=0; i<n;i++){
for(int j=0;j<m;j++){
if (maxvalue<matrix[i][j]){
maxvalue=matrix[i][j];
}
}
}

int array[]=new int[maxvalue+1];
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
array[matrix[i][j]]++;

}
}
for(int i=1;i<=maxvalue+1;i++){
if (array[i]==1){
System.out.print(i +" ");
flag=1;
}
}
if (flag==0){
System.out.println("NO UNIQUE ELEMENT");
}
}

public static void main(String[] args) {
int matrix[][] = {{1,2,3,4},
{2,6,7,8},
{9,8,10,11},
{11,12,13,17}};
try{
uelement(matrix,rows,columns);
}catch(Exception e){
System.out.println("unique elements");
}
}
}```

## Steps to find unique element

1. First of all, we will create a package and then a class.
2. Then we will declare static variables rows and columns. (static variable is the variable whose value can not be changed in the entire program we can say that a single copy of the static variable is created and shared among all the instances of the class.)
3. Then we will create uelement(), a parameterized method in which 1st parameter is the name of the matrix, 2nd parameter is the number of rows and the 3rd element is the number of columns.
4. In the created method first, we will find the maximum element.
5. Then we will create the array of size maximum element+1.
6. Then we will enter all the elements in the array we created.
7. We will traverse the entire array and will find unique elements.
8. If there is no unique element ie.(flag=0) we will display the message ” NO UNIQUE ELEMENTS “.
9. In the main method, we will declare the matrix, or we can take a matrix from the user using the scanner class.
10. After declaring the matrix, we will call the method uelement with the parameters described above.
11. The try-catch block is used to handle the Exceptions.

### Output 