This article explores different ways to merge multiple arrays in Kotlin into a single array. The solution should maintain the original order of elements in individual arrays.

1. Using Plus Operator

A simple and fairly efficient solution to combine two arrays in Kotlin is with the plus operator. This is demonstrated below:

Download Code

 
An equivalent way is to use the plus() function.

2. Using copyOf() with System.arraycopy() function

The idea is to allocate enough memory to the new array for accommodating all elements present in all arrays using the copyOf() function. Then use System.arraycopy() to copy the given arrays into the new array, as shown below:

Download Code

3. Using MutableList

We can also use a MutableList to merge multiple arrays in Kotlin, as shown below. This approach uses the addAll() function:

Download Code

That’s all about merging multiple arrays in Kotlin.