Skip to content

Instantly share code, notes, and snippets.

@Deityhub
Created August 27, 2022 15:44
Show Gist options
  • Save Deityhub/450c6f0d70998a0991e66de015c816eb to your computer and use it in GitHub Desktop.
Save Deityhub/450c6f0d70998a0991e66de015c816eb to your computer and use it in GitHub Desktop.
Rotate a 2D array matrix by 90 degrees
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