This one, is not only a super efficient one, but a pretty short solution.
Algorithm Time Complexity: O[n log n]
const matrix = [
[1,1,1,1],
[2,2,2,2],
[3,3,3,3],
[4,4,4,4]
];
matrix.every[[r, i, a] => [
r.every[[_, j] => [
j = a.length-j-1,
[ r[j], a[j][i] ] = [ a[j][i], r[j] ],
i < j-1
]],
i < length-2
]];
console.log[matrix];
/*
Prints:
[
[1,2,3,4],
[1,2,3,4],
[1,2,3,4],
[1,2,3,4]
]
*/
The example above will do only 6 iterations.
For bigger matrix, say 100x100 it will do 4,900 iterations, this is 51% faster than any other solution provided here.
The principle is simple, you on only iterate through the upper diagonal half of the matrix, because the diagonal line never changes and the bottom diagonal half being is switched together with the upper one, so there is no reason to iterate through it as well. This way, you save a lot of running time, especially in a large matrix.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given a 2D array [matrix] and the task is to get the transpose of the matrix using JavaScript.
Approach 1:
- Store a 2D array into variable.
- Display the 2D array [matrix] content.
- Call map[] method which provides a callback function single time for every element in an array, maintaining the order, and returns a new array [transpose of original array] from the results.
Example: This example uses array.map[] method to get the transpose of the array.
HTML
Transpose of a 2D array
GeeksForGeeks
Click here
var el_up = document.getElementById["GFG_UP"];
var el_down = document.getElementById["GFG_DOWN"];
var array = [
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
];
el_up.innerHTML = "[ [ " + array[0] + " ] ], [ [ "
+ array[1] + " ] ], [ [ " + array[2] + " ] ]";
function transpose[mat] {
for [var i = 0; i <
mat.length
; i++] {
for [var
j
=
0
; j < i; j++] {
const
tmp
=
mat
[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = tmp;
}
}
}
function gfg_Run[] {
array
= array[0].map[[col, i] => array.map[row => row[i]]];
el_down.innerHTML = "[ [ " + array[0] + " ] ], [ [ "
+ array[1] + " ] ], [ [ " + array[2] + " ] ]";
}
Output:
- Before clicking on the button:
- After clicking on the button:
Approach 2:
- Store a 2D array into variable.
- Replace every element in the array by its mirror image with respect to diagonal of the array.
Example: This example creates a function which replaces every element by its mirror image to get the transpose of the array.
HTML
Transposing a 2D array
GeeksForGeeks
Click here
var el_up = document.getElementById["GFG_UP"];
var el_down = document.getElementById["GFG_DOWN"];
var array = [
[1, 1, 1],
[2, 2, 2],
[3, 3, 3],
];
el_up.innerHTML = "[ [ " + array[0] + " ] ], [ [ "
+ array[1] + " ] ], [ [ " + array[2] + " ] ]";
function transpose[mat] {
for [var i = 0; i <
mat.length
; i++] {
for [var
j
=
0
; j < i; j++] {
const
tmp
=
mat
[i][j];
mat[i][j] = mat[j][i];
mat[j][i] = tmp;
}
}
}
function gfg_Run[] {
transpose[array];
el_down.innerHTML
=
"[ [ "
+ array[0] + " ] ], [ [ "
+ array[1] + " ] ], [ [ " + array[2] + " ] ]";
}
Output:
- Before
clicking on the button:
- After clicking on the button: