Remove all zero | Function to remove the rows or columns from the matrix which contains all 0 s elements ; Stores count of rows ; col [ i ] : Stores count of 0 s in current column ; row [ i ] : Stores count of 0 s in current row ; Traverse the matrix ; Stores count of 0 s in current row ; Update col [ j ] ; Update count ; Update row [ i ] ; Traverse the matrix ; If all elements of current row is 0 ; If all elements of current column is 0 ; Driver Code ; Function Call
function removeZeroRowCol ( arr ) { let n = arr . length ; let col = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; let row = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; ++ i ) { let count = 0 ; for ( let j = 0 ; j < n ; ++ j ) { if ( arr [ i ] [ j ] == 1 ) col [ j ] += 1 ; else col [ j ] += 0 ; if ( arr [ i ] [ j ] == 1 ) count += 1 ; else count += 0 ; } row [ i ] = count ; } for ( let i = 0 ; i < n ; ++ i ) { if ( row [ i ] == 0 ) { continue ; } for ( let j = 0 ; j < n ; ++ j ) { if ( col [ j ] != 0 ) document . write ( arr [ i ] [ j ] ) ; } document . write ( " " ) ; } } let arr = [ [ 1 , 1 , 0 , 1 ] , [ 0 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 1 ] ] ; removeZeroRowCol ( arr ) ;