How to rearrange a given unsorted array in maximum minimum form with TypeScript

1 Answer

0 votes
const array : number[] = [4, 9, 3, 5, 1, 2, 8, 6, 7];

array.sort((a, b) => a - b);
 
for (let i : number = 0; i < array.length; i += 2) {
    array.splice(i, 0, array.pop());
}
         
console.log(array);
 
 
 
 
/*
run:
 
[9, 1, 8, 2, 7, 3, 6, 4, 5]
 
*/

 



answered Sep 10, 2022 by avibootz
...