...

/

Solution Review: Right Rotate an Array by One

Solution Review: Right Rotate an Array by One

This review provides a detailed analysis that helps solve the "Right Rotate an Array by One" challenge.

Solution #

Press + to interact
fn rotate_array(arr: &mut[i32]) -> &[i32]{
let last_element = arr[arr.len()-1];
for x in (1..arr.len()).rev(){
arr[x] = arr[x-1];
}
arr[0] = last_element;
arr
}

Explanation

You iterated over the whole array and stored the next element of the array at the previous index. Lastly, you append the last element arr[arr.len()-1]to the 0 ...

Access this course and 1400+ top-rated courses and projects.