Given a binary matrix, print all unique rows of the given matrix.
Example:
Input:
{0, 1, 0, 0, 1}
{1, 0, 1, 1, 0}
{0, 1, 0, 0, 1}
{1, 1, 1, 0, 0}
Output:
0 1 0 0 1
1 0 1 1 0
1 1 1 0 0
Explanation:
The rows are r1={0, 1, 0, 0, 1},
r2={1, 0, 1, 1, 0}, r3={0, 1, 0, 0, 1},
r4={1, 1, 1, 0, 0}, As r1 = r3, remove r3
and print the other rows.
Input:
{0, 1, 0}
{1, 0, 1}
{0, 1, 0}
Output:
0 1 0
1 0 1
Explanation:
The rows are r1={0, 1, 0},
r2={1, 0, 1}, r3={0, 1, 0} As r1 = r3,
remove r3 and print the other rows.
Method 1: This method explains the simple approach towards solving the above problem.
Approach: A simple approach would be to check each row with all processed rows. Print the first row. Now, starting from the second row, for each row, compare the row with already processed rows. If the row matches with any of the processed rows, skip it else print it.
Algorithm:
- Traverse the matrix row-wise
- For each row check if there is any similar row less than the current index.
- If any two rows are similar then do not print the row.
- Else print the row.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
void findUniqueRows( int M[ROW][COL])
{
for ( int i=0; i<ROW; i++)
{
int flag=0;
for ( int j=0; j<i; j++)
{
flag=1;
for ( int k=0; k<=COL; k++)
if (M[i][k]!=M[j][k])
flag=0;
if (flag==1)
break ;
}
if (flag==0)
{
for ( int j=0; j<COL; j++)
cout<<M[i][j]<< " " ;
cout<<endl;
}
}
}
int main()
{
int M[ROW][COL] = {{0, 1, 0, 0, 1},
{1, 0, 1, 1, 0},
{0, 1, 0, 0, 1},
{1, 0, 1, 0, 0}};
findUniqueRows(M);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int ROW = 4 ;
static int COL = 5 ;
static void findUniqueRows( int M[][])
{
for ( int i = 0 ; i < ROW; i++)
{
int flag = 0 ;
for ( int j = 0 ; j < i; j++)
{
flag = 1 ;
for ( int k = 0 ; k < COL; k++)
if (M[i][k] != M[j][k])
flag = 0 ;
if (flag == 1 )
break ;
}
if (flag == 0 )
{
for ( int j = 0 ; j < COL; j++)
System.out.print(M[i][j] + " " );
System.out.println();
}
}
}
public static void main(String[] args)
{
int M[][] = { { 0 , 1 , 0 , 0 , 1 },
{ 1 , 0 , 1 , 1 , 0 },
{ 0 , 1 , 0 , 0 , 1 },
{ 1 , 0 , 1 , 0 , 0 } };
findUniqueRows(M);
}
}
|
Python3
ROW = 4
COL = 5
def findUniqueRows(M):
for i in range (ROW):
flag = 0
for j in range (i):
flag = 1
for k in range (COL):
if (M[i][k] ! = M[j][k]):
flag = 0
if (flag = = 1 ):
break
if (flag = = 0 ):
for j in range (COL):
print (M[i][j], end = " " )
print ()
if __name__ = = '__main__' :
M = [ [ 0 , 1 , 0 , 0 , 1 ],
[ 1 , 0 , 1 , 1 , 0 ],
[ 0 , 1 , 0 , 0 , 1 ],
[ 1 , 0 , 1 , 0 , 0 ] ]
findUniqueRows(M)
|
C#
using System;
class GFG{
static int ROW = 4;
static int COL = 5;
static void findUniqueRows( int [,] M)
{
for ( int i = 0; i < ROW; i++)
{
int flag = 0;
for ( int j = 0; j < i; j++)
{
flag = 1;
for ( int k = 0; k < COL; k++)
if (M[i, k] != M[j, k])
flag = 0;
if (flag == 1)
break ;
}
if (flag == 0)
{
for ( int j = 0; j < COL; j++)
Console.Write(M[i, j] + " " );
Console.WriteLine();
}
}
}
static void Main()
{
int [,] M = { { 0, 1, 0, 0, 1 },
{ 1, 0, 1, 1, 0 },
{ 0, 1, 0, 0, 1 },
{ 1, 0, 1, 0, 0 } };
findUniqueRows(M);
}
}
|
Javascript
<script>
let ROW = 4;
let COL = 5;
function findUniqueRows(M)
{
for (let i = 0; i < ROW; i++)
{
let flag = 0;
for (let j = 0; j < i; j++)
{
flag = 1;
for (let k = 0; k < COL; k++)
if (M[i][k] != M[j][k])
flag = 0;
if (flag == 1)
break ;
}
if (flag == 0)
{
for (let j = 0; j < COL; j++)
document.write(M[i][j] + " " );
document.write( "<br>" );
}
}
}
let M = [ [ 0, 1, 0, 0, 1 ],
[ 1, 0, 1, 1, 0 ],
[ 0, 1, 0, 0, 1 ],
[ 1, 0, 1, 0, 0 ] ]
findUniqueRows(M)
</script>
|
Output
0 1 0 0 1
1 0 1 1 0
1 0 1 0 0
Complexity Analysis:
- Time complexity: O( ROW^2 x COL ).
So for every row check if there is any other similar row. So the time complexity is O( ROW^2 x COL ).
- Auxiliary Space: O(1).
As no extra space is required.
Method 2: This method uses Binary Search Tree to solve the above operation. The Binary Search Tree is a node-based binary tree data structure which has the following properties:
- The left subtree of a node contains only nodes with keys lesser than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- The left and right subtree each must also be a binary search tree.
- There must be no duplicate nodes.
The above properties of Binary Search Tree provide ordering among keys so that the operations like search, minimum and maximum can be done fast. If there is no order, then we may have to compare every key to search a given key.
Approach: The process must begin from finding the decimal equivalent of each row and inserting them into a BST. As we know, each node of the BST will contain two fields, one field for the decimal value, other for row number. One must not insert a node if it is duplicated. Finally, traverse the BST and print the corresponding rows.
Algorithm:
- Create a BST in which no duplicate elements can be stored. Create a function to convert a row into decimal and to convert the decimal value into binary array.
- Traverse through the matrix and insert the row into the BST.
- Traverse the BST (inorder traversal) and convert the decimal into binary array and print it.
Implementation:
C++14
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
class BST
{
int data;
BST *left, *right;
public :
BST();
BST( int );
BST* Insert(BST *, int );
void Inorder(BST *);
};
int convert( int arr[])
{
int sum=0;
for ( int i=0; i<COL; i++)
{
sum+= pow (2,i)*arr[i];
}
return sum;
}
void print( int p)
{
for ( int i=0; i<COL; i++)
{
cout<<p%2<< " " ;
p/=2;
}
cout<<endl;
}
BST :: BST() : data(0), left(NULL), right(NULL){}
BST :: BST( int value)
{
data = value;
left = right = NULL;
}
BST* BST :: Insert(BST *root, int value)
{
if (!root)
{
return new BST(value);
}
if (value == root->data)
return root;
if (value > root->data)
{
root->right = Insert(root->right, value);
}
else
{
root->left = Insert(root->left, value);
}
return root;
}
void BST :: Inorder(BST *root)
{
if (!root)
{
return ;
}
Inorder(root->left);
print( root->data );
Inorder(root->right);
}
void findUniqueRows( int M[ROW][COL])
{
BST b, *root = NULL;
for ( int i=0; i<ROW; i++)
{
root=b.Insert(root,convert(M[i]));
}
b.Inorder(root);
}
int main()
{
int M[ROW][COL] = {{0, 1, 0, 0, 1},
{1, 0, 1, 1, 0},
{0, 1, 0, 0, 1},
{1, 0, 1, 0, 0}};
findUniqueRows(M);
return 0;
}
|
Java
import java.util.*;
class GFG{
static class BST {
int data;
BST left,right;
BST( int v){
this .data = v;
this .left = this .right = null ;
}
}
final static int ROW = 4 ;
final static int COL = 5 ;
static int convert( int arr[])
{
int sum = 0 ;
for ( int i = 0 ; i < COL; i++)
{
sum += Math.pow( 2 ,i)*arr[i];
}
return sum;
}
static void print( int p)
{
for ( int i = 0 ; i < COL; i++)
{
System.out.print(p% 2 + " " );
p /= 2 ;
}
System.out.println();
}
static BST Insert(BST root, int value)
{
if (root == null )
{
return new BST(value);
}
if (value == root.data)
return root;
if (value > root.data)
{
root.right = Insert(root.right, value);
}
else
{
root.left = Insert(root.left, value);
}
return root;
}
static void Inorder(BST root)
{
if (root == null )
{
return ;
}
Inorder(root.left);
print( root.data );
Inorder(root.right);
}
static void findUniqueRows( int M[][])
{
BST b, root = null ;
for ( int i = 0 ; i < ROW; i++)
{
root=Insert(root, convert(M[i]));
}
Inorder(root);
}
public static void main(String[] args)
{
int M[][] = {{ 0 , 1 , 0 , 0 , 1 },
{ 1 , 0 , 1 , 1 , 0 },
{ 0 , 1 , 0 , 0 , 1 },
{ 1 , 0 , 1 , 0 , 0 }};
findUniqueRows(M);
}
}
|
C#
using System;
using System.Collections.Generic;
public class GFG{
public class BST {
public int data;
public BST left,right;
public BST( int v){
this .data = v;
this .left = this .right = null ;
}
}
readonly static int ROW = 4;
readonly static int COL = 5;
static int convert( int []arr)
{
int sum = 0;
for ( int i = 0; i < COL; i++)
{
sum += ( int )Math.Pow(2,i)*arr[i];
}
return sum;
}
static void print( int p)
{
for ( int i = 0; i < COL; i++)
{
Console.Write(p%2+ " " );
p /= 2;
}
Console.WriteLine();
}
static BST Insert(BST root, int value)
{
if (root == null )
{
return new BST(value);
}
if (value == root.data)
return root;
if (value > root.data)
{
root.right = Insert(root.right, value);
}
else
{
root.left = Insert(root.left, value);
}
return root;
}
static void Inorder(BST root)
{
if (root == null )
{
return ;
}
Inorder(root.left);
print( root.data );
Inorder(root.right);
}
public static int [] GetRow( int [,] matrix, int row)
{
var rowLength = matrix.GetLength(1);
var rowVector = new int [rowLength];
for ( var i = 0; i < rowLength; i++)
rowVector[i] = matrix[row, i];
return rowVector;
}
static void findUniqueRows( int [,]M)
{
BST b, root = null ;
for ( int i = 0; i < ROW; i++)
{
int [] row = GetRow(M,i);
root=Insert(root, convert(row));
}
Inorder(root);
}
public static void Main(String[] args)
{
int [,]M = {{0, 1, 0, 0, 1},
{1, 0, 1, 1, 0},
{0, 1, 0, 0, 1},
{1, 0, 1, 0, 0}};
findUniqueRows(M);
}
}
|
Python3
ROW = 4
COL = 5
def Print (p):
for i in range (COL):
print (p % 2 ,end = " " )
p = int (p / / 2 )
print ("")
class BST:
def __init__( self ,data):
self .data = data
self .left = None
self .right = None
def Insert( self ,root, value):
if ( not root):
return BST(value)
if (value = = root.data):
return root
if (value > root.data):
root.right = self .Insert(root.right, value)
else :
root.left = self .Insert(root.left, value)
return root
def Inorder( self ,root):
if ( not root):
return
self .Inorder(root.left);
Print ( root.data );
self .Inorder(root.right)
def convert(arr):
sum = 0
for i in range (COL):
sum + = pow ( 2 ,i) * arr[i]
return sum
def findUniqueRows(M):
b,root = BST( 0 ), None
for i in range (ROW):
root = b.Insert(root,convert(M[i]))
b.Inorder(root)
M = [[ 0 , 1 , 0 , 0 , 1 ],
[ 1 , 0 , 1 , 1 , 0 ],
[ 0 , 1 , 0 , 0 , 1 ],
[ 1 , 0 , 1 , 0 , 0 ]]
findUniqueRows(M)
|
Javascript
<script>
var ROW = 4
var COL = 5
class BST
{
constructor(data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
Insert(root, value)
{
if (!root)
{
return new BST(value);
}
if (value == root.data)
return root;
if (value > root.data)
{
root.right = this .Insert(root.right, value);
}
else
{
root.left = this .Insert(root.left, value);
}
return root;
}
Inorder(root)
{
if (!root)
{
return ;
}
this .Inorder(root.left);
print( root.data );
this .Inorder(root.right);
}
};
function convert(arr)
{
var sum=0;
for ( var i=0; i<COL; i++)
{
sum+=Math.pow(2,i)*arr[i];
}
return sum;
}
function print(p)
{
for ( var i=0; i<COL; i++)
{
document.write(p%2 + " " );
p=parseInt(p/2);
}
document.write( "<br>" );
}
function findUniqueRows(M)
{
var b = new BST(0),root = null ;
for ( var i=0; i<ROW; i++)
{
root=b.Insert(root,convert(M[i]));
}
b.Inorder(root);
}
var M = [[0, 1, 0, 0, 1],
[1, 0, 1, 1, 0],
[0, 1, 0, 0, 1],
[1, 0, 1, 0, 0]];
findUniqueRows(M);
</script>
|
Output
1 0 1 0 0
1 0 1 1 0
0 1 0 0 1
Complexity Analysis:
- Time complexity: O( ROW x COL + ROW x log( ROW ) ).
To traverse the matrix time complexity is O( ROW x COL) and to insert them into BST time complexity is O(log ROW) for each row. So overall time complexity is O( ROW x COL + ROW x log( ROW ) )
- Auxiliary Space: O( ROW ).
To store the BST O(ROW) space is needed.
Method 3: This method uses Trie data structure to solve the above problem. Trie is an efficient information retrieval data structure. Using Trie, search complexities can be brought to an optimal limit (key length). If we store keys in the binary search tree, a well-balanced BST will need time proportional to M * log N, where M is maximum string length and N is the number of keys in the tree. Using Trie, we can search the key in O(M) time. However, the penalty is on Trie storage requirements.
Note: This method will lead to Integer Overflow if the number of columns is large.
Approach:
Since the matrix is boolean, a variant of Trie data structure can be used where each node will be having two children one for 0 and other for 1. Insert each row in the Trie. If the row is already there, don’t print the row. If the row is not there in Trie, insert it in Trie and print it.
Algorithm:
- Create a Trie where rows can be stored.
- Traverse through the matrix and insert the row into the Trie.
- Trie cannot store duplicate entries so the duplicates will be removed
- Traverse the Trie and print the rows.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
#define ROW 4
#define COL 5
class Node
{
public :
bool isEndOfCol;
Node *child[2];
} ;
Node* newNode()
{
Node* temp = new Node();
temp->isEndOfCol = 0;
temp->child[0] = temp->child[1] = NULL;
return temp;
}
bool insert(Node** root, int (*M)[COL],
int row, int col )
{
if (*root == NULL)
*root = newNode();
if (col < COL)
return insert (&((*root)->child[M[row][col]]),
M, row, col + 1);
else
{
if (!((*root)->isEndOfCol))
return (*root)->isEndOfCol = 1;
return 0;
}
}
void printRow( int (*M)[COL], int row)
{
int i;
for (i = 0; i < COL; ++i)
cout << M[row][i] << " " ;
cout << endl;
}
void findUniqueRows( int (*M)[COL])
{
Node* root = NULL;
int i;
for (i = 0; i < ROW; ++i)
if (insert(&root, M, i, 0))
printRow(M, i);
}
int main()
{
int M[ROW][COL] = {{0, 1, 0, 0, 1},
{1, 0, 1, 1, 0},
{0, 1, 0, 0, 1},
{1, 0, 1, 0, 0}};
findUniqueRows(M);
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define ROW 4
#define COL 5
typedef struct Node
{
bool isEndOfCol;
struct Node *child[2];
} Node;
Node* newNode()
{
Node* temp = (Node *) malloc ( sizeof ( Node ) );
temp->isEndOfCol = 0;
temp->child[0] = temp->child[1] = NULL;
return temp;
}
bool insert( Node** root, int (*M)[COL], int row, int col )
{
if ( *root == NULL )
*root = newNode();
if ( col < COL )
return insert ( &( (*root)->child[ M[row][col] ] ), M, row, col+1 );
else
{
if ( !( (*root)->isEndOfCol ) )
return (*root)->isEndOfCol = 1;
return 0;
}
}
void printRow( int (*M)[COL], int row )
{
int i;
for ( i = 0; i < COL; ++i )
printf ( "%d " , M[row][i] );
printf ( "\n" );
}
void findUniqueRows( int (*M)[COL] )
{
Node* root = NULL;
int i;
for ( i = 0; i < ROW; ++i )
if ( insert(&root, M, i, 0) )
printRow( M, i );
}
int main()
{
int M[ROW][COL] = {{0, 1, 0, 0, 1},
{1, 0, 1, 1, 0},
{0, 1, 0, 0, 1},
{1, 0, 1, 0, 0}
};
findUniqueRows( M );
return 0;
}
|
Java
import java.util.*;
class GFG {
static class Node {
boolean isEndOfCol;
Node[] child = new Node[ 2 ];
}
static class Trie {
public static Node NewNode()
{
Node temp = new Node();
temp.isEndOfCol = false ;
temp.child[ 0 ] = temp.child[ 1 ] = null ;
return temp;
}
public static boolean Insert(Node root, int [][] M,
int row, int col)
{
if (root == null )
root = NewNode();
if (col < M[ 0 ].length)
return Insert(root.child[M[row][col]], M,
row, col + 1 );
else
{
if (!(root.isEndOfCol))
return root.isEndOfCol = true ;
return false ;
}
}
public static void PrintRow( int [][] M, int row)
{
for ( int i = 0 ; i < M[ 0 ].length; ++i)
System.out.print(M[row][i] + " " );
System.out.println();
}
public static void FindUniqueRows( int [][] M)
{
Node root = null ;
for ( int i = 0 ; i < M.length; ++i)
if (Insert(root, M, i, 0 ))
PrintRow(M, i);
}
}
public static void main(String[] args)
{
int [][] M = { { 0 , 1 , 0 , 0 , 1 },
{ 1 , 0 , 1 , 1 , 0 },
{ 0 , 1 , 0 , 0 , 1 },
{ 1 , 0 , 1 , 0 , 0 } };
Trie.FindUniqueRows(M);
System.out.println();
}
}
|
Python3
class Node:
def __init__( self ):
self .isEndOfCol = False
self .child = [ None , None ]
def newNode():
temp = Node()
return temp
def insert(root, M, row, col):
if root is None :
root = newNode()
if col < COL:
return insert(root.child[M[row][col]], M, row, col + 1 )
else :
if not root.isEndOfCol:
root.isEndOfCol = True
return True
return False
def printRow(row):
for i in row:
print (i, end = " " )
print ()
def findUniqueRows(M):
unique_rows = []
for i in range (ROW):
if not any (M[i] = = row for row in unique_rows):
unique_rows.append(M[i])
for row in unique_rows:
printRow(row)
ROW = 4
COL = 5
M = [[ 0 , 1 , 0 , 0 , 1 ],
[ 1 , 0 , 1 , 1 , 0 ],
[ 0 , 1 , 0 , 0 , 1 ],
[ 1 , 0 , 1 , 0 , 0 ]]
findUniqueRows(M)
|
Javascript
let ROW = 4
let COL = 5
class Node
{
constructor()
{
this .isEndOfCol;
this .child = new Array(2);
}
} ;
function newNode()
{
let temp = new Node();
temp.isEndOfCol = 0;
temp.child[0] = null ;
temp.child[1] = null ;
return temp;
}
function insert(root, M, row, col)
{
if (root == null )
root = newNode();
if (col < (M.length).length)
return insert(((root).child[M[row][col]]),
M, row, col + 1);
else
{
if (!((root).isEndOfCol))
{
(root).isEndOfCol = 1;
return 1;
}
}
return 0;
}
function printRow(M, row)
{
console.log(M[row].join( " " ))
}
function findUniqueRows(M)
{
let root = null ;
let i;
for (i = 0; i < ROW; ++i)
if (insert(root, M, i, 0))
printRow(M, i);
}
let M = [[0, 1, 0, 0, 1], [1, 0, 1, 1, 0], [0, 1, 0, 0, 1], [1, 0, 1, 0, 0]];
findUniqueRows(M);
|
C#
using System;
namespace Trie
{
class Node
{
public bool isEndOfCol;
public Node[] child = new Node[2];
}
class Trie
{
public static Node NewNode()
{
Node temp = new Node();
temp.isEndOfCol = false ;
temp.child[0] = temp.child[1] = null ;
return temp;
}
public static bool Insert( ref Node root, int [,] M,
int row, int col )
{
if (root == null )
root = NewNode();
if (col < M.GetLength(1))
return Insert ( ref root.child[M[row, col]], M, row, col + 1);
else
{
if (!(root.isEndOfCol))
return root.isEndOfCol = true ;
return false ;
}
}
public static void PrintRow( int [,] M, int row)
{
for ( int i = 0; i < M.GetLength(1); ++i)
Console.Write(M[row, i] + " " );
Console.WriteLine();
}
public static void FindUniqueRows( int [,] M)
{
Node root = null ;
for ( int i = 0; i < M.GetLength(0); ++i)
if (Insert( ref root, M, i, 0))
PrintRow(M, i);
}
}
class GFG
{
static void Main( string [] args)
{
int [,] M = {{0, 1, 0, 0, 1},
{1, 0, 1, 1, 0},
{0, 1, 0, 0, 1},
{1, 0, 1, 0, 0}};
Trie.FindUniqueRows(M);
Console.ReadLine();
}
}
}
|
Output
0 1 0 0 1
1 0 1 1 0
1 0 1 0 0
Complexity Analysis:
- Time complexity: O( ROW x COL ).
To traverse the matrix and insert in the trie the time complexity is O( ROW x COL). This method has better time complexity. Also, the relative order of rows is maintained while printing but it takes a toll on space.
- Auxiliary Space: O( ROW x COL ).
To store the Trie O(ROW x COL) space complexity is needed.
Method 4: This method uses HashSet data structure to solve the above problem. The HashSet class implements the Set interface, backed by a hash table which is actually a HashMap instance. No guarantee is made as to the iteration order of the set which means that the class does not guarantee the constant order of elements over time. This class permits the null element. The class offers constant time performance for the basic operations like add, remove, contains and size assuming the hash function disperses the elements properly among the buckets.
Approach: In this method convert the whole row into a single String and then if check it is already present in the HashSet or not. If the row is present then we will leave it otherwise we will print unique row and add it to HashSet.
Algorithm:
- Create a HashSet where rows can be stored as a String.
- Traverse through the matrix and insert the row as String into the HashSet.
- HashSet cannot store duplicate entries so the duplicates will be removed
- Traverse the HashSet and print the rows.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
void printArray( int arr[][5], int row,
int col)
{
unordered_set<string> uset;
for ( int i = 0; i < row; i++)
{
string s = "" ;
for ( int j = 0; j < col; j++)
s += to_string(arr[i][j]);
if (uset.count(s) == 0)
{
uset.insert(s);
cout << s << endl;
}
}
}
int main()
{
int arr[][5] = {{0, 1, 0, 0, 1},
{1, 0, 1, 1, 0},
{0, 1, 0, 0, 1},
{1, 1, 1, 0, 0}};
printArray(arr, 4, 5);
}
|
Java
import java.util.HashSet;
public class GFG {
public static void printArray( int arr[][],
int row, int col)
{
HashSet<String> set = new HashSet<String>();
for ( int i = 0 ; i < row; i++)
{
String s = "" ;
for ( int j = 0 ; j < col; j++)
s += String.valueOf(arr[i][j]);
if (!set.contains(s)) {
set.add(s);
System.out.println(s);
}
}
}
public static void main(String[] args) {
int arr[][] = { { 0 , 1 , 0 , 0 , 1 },
{ 1 , 0 , 1 , 1 , 0 },
{ 0 , 1 , 0 , 0 , 1 },
{ 1 , 1 , 1 , 0 , 0 } };
printArray(arr, 4 , 5 );
}
}
|
Python3
def printArray(matrix):
rowCount = len (matrix)
if rowCount = = 0 :
return
columnCount = len (matrix[ 0 ])
if columnCount = = 0 :
return
row_output_format = " " .join([ "%s" ] * columnCount)
printed = {}
for row in matrix:
routput = row_output_format % tuple (row)
if routput not in printed:
printed[routput] = True
print (routput)
mat = [[ 0 , 1 , 0 , 0 , 1 ],
[ 1 , 0 , 1 , 1 , 0 ],
[ 0 , 1 , 0 , 0 , 1 ],
[ 1 , 1 , 1 , 0 , 0 ]]
printArray(mat)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static void printArray( int [][] arr, int row, int col)
{
HashSet< string > set = new HashSet< string >();
for ( int i = 0; i < row; i++)
{
string s = "" ;
for ( int j = 0; j < col; j++)
{
s += arr[i][j].ToString();
}
if (! set .Contains(s))
{
set .Add(s);
Console.WriteLine(s);
}
}
}
public static void Main( string [] args)
{
int [][] arr = new int [][]
{
new int [] {0, 1, 0, 0, 1},
new int [] {1, 0, 1, 1, 0},
new int [] {0, 1, 0, 0, 1},
new int [] {1, 1, 1, 0, 0}
};
printArray(arr, 4, 5);
}
}
|
Javascript
<script>
function printArray(arr,row,col)
{
let set = new Set();
for (let i = 0; i < row; i++)
{
let s = "" ;
for (let j = 0; j < col; j++)
s += (arr[i][j]).toString();
if (!set.has(s)) {
set.add(s);
document.write(s+ "<br>" );
}
}
}
let arr = [[0, 1, 0, 0, 1],
[1, 0, 1, 1, 0],
[0, 1, 0, 0, 1],
[1, 1, 1, 0, 0]];
printArray(arr, 4, 5);
</script>
|
Complexity Analysis:
- Time complexity: O( ROW x COL ).
To traverse the matrix and insert in the HashSet the time complexity is O( ROW x COL)
- Auxiliary Space: O( ROW ).
To store the HashSet O(ROW x COL) space complexity is needed.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!