Answer the question
In order to leave comments, you need to log in
How to rearrange the elements of an array from the end to the beginning?
There is a function that takes an array and a number n
. The function must rearrange n
the elements of the array from the end to the beginning.
Example:
console.log(
moveToStart ([1, 2, 3, 4, 5], 3)
); // [3, 4, 5, 1, 2]
Answer the question
In order to leave comments, you need to log in
function moveToStart(array, n) {
return [...array.slice(-n),...array.slice(0, array.length - n)]
}
function moveToStart(arr, n) {
if(n && n < arr.length) {
while(n--)
arr.unshift(arr.pop());
}
return arr;
}
console.log(moveToStart([1, 2, 3, 4, 5], 3)); // [3, 4, 5, 1, 2]
slice
function moveToStart(arr, n) {
const amount = arr.length - n;
return arr.slice(amount).concat(arr.slice(0, amount));
}
console.log(moveToStart([1, 2, 3, 4, 5], 3)); // [3, 4, 5, 1, 2]
const moveToStart = (arr, n) => [ ...arr.splice(-n), ...arr ];
Most of the solutions already proposed here work for a square and already for n=10000 amount=5000 they will work for a noticeably long time. Of course, it is unlikely that such a large amount of data will be on the client, but even with n=100, if the function is executed often, why make it ~50 times slower?
The solution suggested by Paul is the best. It works fast and, like many algorithmically correct solutions, it is much shorter and more readable than other solutions.
If you want without splice(), then you need to create a new array and then copy the elements n-amount..n-1 into it at the beginning with one cycle, then copy the elements 0..n-amount-1 further with the second cycle.
The interface you fixed suggests using a new array for the results each time. But it could be done without using a lot of additional memory, hanging the array itself in place, if we allow the interface to change.
True, splice would no longer be usable. Still it would be necessary to know the theory of permutations a little. In short, here is a solution that works behind the line and shuffles the array in place:
function gcd(a,b) {
if (a>b) return gcd(b,a);
return a == 0? b : gcd(b%a, a);
}
function next(i, k) {
return i >= k ? i-k : i+n-k
}
function moveToStartInPlace(arr, k) {
n = arr.length;
num_cycles = gcd(k,n)
for (i=0; i < num_cycles; ++i) {
saved_value = arr[i];
j = i;
nxt = next(j, k);
while (nxt != i) {
arr[j] = arr[nxt];
j = nxt;
nxt = next(j, k);
}
arr[j] = saved_value;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question