DEV Community

Bukunmi Odugbesan
Bukunmi Odugbesan

Posted on

Coding Challenge Practice - Question 50

The task is to implement a function to merge sorted arrays of non-descending integers.

The boilerplate code

function merge(arrList) {
  // your code here
}
Enter fullscreen mode Exit fullscreen mode

Flatten all the arrays into one

const merged = [].concat(...arrList)
Enter fullscreen mode Exit fullscreen mode

.concat() is used to concatenate arrays. Next, sort the array in ascending order.

merged.sort((a,b) => a - b)
Enter fullscreen mode Exit fullscreen mode

The final code

function merge(arrList) {
  // your code here
  const merged = [].concat(...arrList)

  merged.sort((a,b) => a - b)

  return merged;
}
Enter fullscreen mode Exit fullscreen mode

That's all folks!

Top comments (0)