Created
August 27, 2022 15:44
-
-
Save Deityhub/450c6f0d70998a0991e66de015c816eb to your computer and use it in GitHub Desktop.
Rotate a 2D array matrix by 90 degrees
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function rotate(arr) { | |
const arrLength = arr.length; | |
let matrix = Array.from({length: arrLength}, () => []); | |
for(let i = 0; i < arr.length; i++) { | |
for(let j = 0; j < arr.length; j++) { | |
let temp = arr[j][i]; | |
matrix[i][arrLength - 1 - j] = temp | |
} | |
} | |
return matrix; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment