How to find unique elements in matrix in C++

In this tutorial, we will learn how to find unique elements in a matrix in C++ with an example and algorithm.

The elements which appear only one time in a matrix are unique elements of that matrix.

How to find unique elements in a matrix

let’s take an example to find a unique element in a matrix manually.

Example: 

Consider a matrix of od size 3*4
input matrix: 
              1 2 3
              6 5 1
              8 3 5
              7 8 9

so, there are 3*4 = 12 elements in the - 1,2,3,5,6,7,8,9
in which 1,3,5,8 are repeated elements and 2,6,7 and 9 are not repeated in the
matrix. 
Therefore, the unique elements in the array are 2,6,7,9.

Algorithm

  1. Declare and initialize a matrix of m*n. (where m and n are the lengths of row and column of matrix).
  2. Find the largest number in the matrix.
  3. declare a temporary array of size equal to largest lumber in the matrix with an increment of 1.
  4. start an outer loop from i = 0 to m and an inner loop from j = 0 to n inside the outer loop.
  5. For each element in the matrix increment the value by one at the temporary array by using the element as an index number.
  6. Traverse the temporary array and search for value 1 in the temporary array. when everyone is the encounter, print the index number.
  7. if no value in the matrix is one, then no unique element present in the array.

C++ program to find unique elements in a matrix

#include<bits/stdc++.h> 
using namespace std;  

//function to calculate unique element 
int FindUnique(int matrix[50][50], int m, int n) 
{ 
  int max = 0, flag = 0; 
  for(int i = 0; i < m; i++) 
    for(int j = 0; j < n; j++) 
      // Find maximum element in 
      // a matrix 
      if(max < matrix[i][j]) 
          max = matrix[i][j]; 

  // Take 1-D array of (maximum + 1) size
  int temp[max + 1] = {0}; 
  //counting the number of repetation of number available 
  //by index value as that number
  for(int i = 0 ; i < m; i++) 
    for(int j = 0; j < n; j++) 
      temp[matrix[i][j]]++; 

  //print unique element 
  cout<<"Unique numbers are: ";
  for(int i = 1; i <= max; i++) 
    if(temp[i] == 1) 
      cout << i << " "; 
      flag = 1; 
      
  if(!flag){ 
    cout << " No unique element in the matrix"; 
  } 
} 

// Driver program 
int main() 
{ 
  int matrix[50][50], m, n;

    //taking input for the length of row and column.
    cout<<"Enter the length of the row: ";
    cin>>m;
    cout<<"Enther the length of the column: ";
    cin>>n;


    //input the elements of matrix
    cout<<"\nEnter the lements of matrix:\n ";
    for(int i = 0;i < m;i++)
        for(int j = 0; j < n; j++)
            cin>>matrix[i][j];
    
  // function that calculate unique element 
  FindUnique(matrix, m, n); 
  return 0; 
}

Output:

Enter the length of the row: 3
Enther the length of the column: 4

Enter the elements of marix: 

1 2 3
6 5 1
8 3 5
7 8 9
Unique numbers are: 2 6 7 9

Time complexity: O(m*n) where m and n is the size of row and column of the matrix.

You may also read: 

  1. Matrix chain multiplication in C++
  2. How to swap both diagonals of a matrix in C++

Leave a Reply

Your email address will not be published. Required fields are marked *