Find unique elements in a matrix

Given a matrix mat[][] having n rows and m columns. We need to find unique elements in matrix i.e, those elements which are not repeated in the matrix or those elements whose frequency is 1.

Examples:

Input :  20  15  30  2
         2   3   5   30
         6   7   6   8
Output : 3  20  5  7  8  15 

Input :  1  2  3  
         5  6  2
         1  3  5
         6  2  2
Output : No unique element in the matrix

Follow these steps to find unique element:
1. Create an empty hash table or dictionary.
2. Traverse through all the elements of the matrix
3. If element is present in the dictionary, then increment its count
4. Otherwise insert element with value = 1.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find unique
// element in matrix
#include<bits/stdc++.h>
using namespace std;
#define R 4
#define C 4
  
// function that calculate unique element
int unique(int mat[R][C], int n, int m)
{
    int maximum = 0, flag = 0;
    for(int i = 0; i < n; i++) 
        for(int j = 0; j < m; j++)
            // Find maximum element in
            // a matrix
            if(maximum < mat[i][j])
                    maximum = mat[i][j];
  
    // Take 1-D array of (maximum + 1)
    // size
    int b[maximum + 1] = {0};
    for(int i = 0 ; i < n; i++)
        for(int j = 0; j < m; j++)
            b[mat[i][j]]++;
  
    //print unique element
    for(int i = 1; i <= maximum; i++)
        if(b[i] == 1)
            cout << i << " ";
            flag = 1;
              
    if(!flag){
        cout << "No unique element in the matrix";
    }
}
  
// Driver program
int main()
{
    int mat[R][C] = {{ 1, 2, 3, 20},
                     {5, 6, 20, 25},
                     {1, 3, 5, 6},
                     {6, 7, 8, 15}};
                       
    // function that calculate unique element
    unique(mat, R, C);
    return 0;
}
  
// This code is contributed by Naman_Garg.

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find unique
// element in matrix
class GFG
{
static int R = 4, C = 4;
  
// function that calculate 
// unique element
static void unique(int mat[][], 
                   int n, int m)
{
    int maximum = 0, flag = 0;
    for(int i = 0; i < n; i++) 
        for(int j = 0; j < m; j++)
          
            // Find maximum element 
            // in a matrix
            if(maximum < mat[i][j])
                    maximum = mat[i][j];
  
    // Take 1-D array of 
    // (maximum + 1) size
    int b[] = new int [maximum + 1];
    for(int i = 0 ; i < n; i++)
        for(int j = 0; j < m; j++)
            b[mat[i][j]]++;
  
    //print unique element
    for(int i = 1; i <= maximum; i++)
        if(b[i] == 1)
            System.out.print(i + " ");
            flag = 1;
              
    if(flag == 0)
    {
        System.out.println("No unique element "
                                "in the matrix");
    }
}
  
// Driver Code
public static void main(String args[])
{
    int mat[][] = {{1, 2, 3, 20},
                   {5, 6, 20, 25},
                   {1, 3, 5, 6},
                   {6, 7, 8, 15}};
                      
    // function that calculate 
    // unique element
    unique(mat, R, C);
}
}
  
// This code is contributed
// by Arnab Kundu

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find unique
# element in matrix
def unique(mat, n, m):
  
    maximum = 0; flag = 0
      
    for i in range(0, n):
        for j in range(0, m):
              
            # Find maximum element in
            # a matrix
            if(maximum < mat[i][j]):
                maximum = mat[i][j];
  
    uniqueElementDict = [0] * (maximum + 1)
  
    # loops to traverse through the matrix 
    for i in range(0, n):
        for j in range(0, m):
                uniqueElementDict[mat[i][j]] += 1
  
    # Print all those keys whose count is 1
    for key in range(maximum + 1):
        if uniqueElementDict[key] == 1:
            print (key, end = " ")
            flag = 1
      
    if(flag == 0):
        print("No unique element in the matrix")
  
# Driver Code
mat = [[1, 2, 3, 20], 
       [5, 6, 20, 25],
       [1, 3, 5, 6],
       [6, 7, 8, 15]]
n = 4
m = 4
unique(mat, n, m)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find unique 
// element in matrix
using System;
  
class GFG 
static int R = 4, C = 4; 
  
// function that calculate 
// unique element 
static void unique(int [,]mat, 
                   int n, int m) 
    int maximum = 0, flag = 0; 
    for(int i = 0; i < n; i++) 
        for(int j = 0; j < m; j++) 
          
            // Find maximum element 
            // in a matrix 
            if(maximum < mat[i, j]) 
                    maximum = mat[i, j]; 
  
    // Take 1-D array of 
    // (maximum + 1) size 
    int []b = new int [maximum + 1]; 
    for(int i = 0 ; i < n; i++) 
        for(int j = 0; j < m; j++) 
            b[mat[i, j]]++; 
  
    // print unique element 
    for(int i = 1; i <= maximum; i++) 
        if(b[i] == 1) 
            Console.Write(i + " "); 
            flag = 1; 
              
    if(flag == 0) 
    
        Console.WriteLine("No unique element "
                               "in the matrix"); 
    
  
// Driver Code 
public static void Main() 
    int [,]mat = {{1, 2, 3, 20}, 
                  {5, 6, 20, 25}, 
                  {1, 3, 5, 6}, 
                  {6, 7, 8, 15}}; 
                      
    // function that calculate 
    // unique element 
    unique(mat, R, C); 
  
// This code is contributed 
// by Subhadeep

chevron_right


PHP


Output:

2 7 8 25 15 


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.