Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I interleave two arrays?

If I have two arrays e.g

let one = [1,3,5]
let two = [2,4,6]

I would like to merge/interleave the arrays in the following pattern [one[0], two[0], one[1], two[1] etc....]

//prints [1,2,3,4,5,6]
let comibned = mergeFunction(one, two)
print(combined)

What would be a good way to implement the combining function?

func mergeFunction(one: [T], _ two: [T]) -> [T] {
    var mergedArray = [T]()
    //What goes here
    return mergedArray
}
like image 961
Neil Horton Avatar asked Jan 22 '16 16:01

Neil Horton


People also ask

How do I combine two arrays?

In order to combine (concatenate) two arrays, we find its length stored in aLen and bLen respectively. Then, we create a new integer array result with length aLen + bLen . Now, in order to combine both, we copy each element in both arrays to result by using arraycopy() function.

What is interleaved array?

Interleaves corresponding elements from the input arrays into a single output array. array 0..n-1 must be 1D. If the input array is not the same size, the number of elements in interleaved array equals the number of elements in the smallest input array multiplied by the number of input arrays.

How do you interleave two lists in Matlab?

Interleaving two matrices by row To do this, you can use the following matlab code: % create two matrices. a = [11 13; 12 14]; b = [21 23; 22 24]; % interleaves two same sized matrices by row row_interleave = reshape([a(:) b(:)]',2*size(a,1), []) % Note that the reshape requires that a and b be the same size.

How do you add two different arrays?

To merge elements from one array to another, we must first iterate(loop) through all the array elements. In the loop, we will retrieve each element from an array and insert(using the array push() method) to another array. Now, we can call the merge() function and pass two arrays as the arguments for merging.


2 Answers

If both arrays have the same length then this is a possible solution:

let one = [1,3,5]
let two = [2,4,6]

let merged = zip(one, two).flatMap { [$0, $1] }

print(merged) // [1, 2, 3, 4, 5, 6]

Here zip() enumerates the arrays in parallel and returns a sequence of pairs (2-element tuples) with one element from each array. flatMap() creates a 2-element array from each pair and concatenates the result.

If the arrays can have different length then you append the extra elements of the longer array to the result:

func mergeFunction<T>(one: [T], _ two: [T]) -> [T] {
    let commonLength = min(one.count, two.count)
    return zip(one, two).flatMap { [$0, $1] } 
           + one.suffixFrom(commonLength)
           + two.suffixFrom(commonLength)
}

Update for Swift 3:

func mergeFunction<T>(_ one: [T], _ two: [T]) -> [T] {
    let commonLength = min(one.count, two.count)
    return zip(one, two).flatMap { [$0, $1] } 
           + one.suffix(from: commonLength)
           + two.suffix(from: commonLength)
}
like image 160
Martin R Avatar answered Sep 30 '22 10:09

Martin R


If you're just looking to interleave two arrays, you could just do something like:

let maxIndex = max(one.count, two.count)
var mergedArray = Array<T>()
for index in 0..<maxIndex {
    if index < one.count { mergedArray.append(one[index]) }
    if index < two.count { mergedArray.append(two[index]) }
}

return mergedArray
like image 35
Charles A. Avatar answered Sep 30 '22 10:09

Charles A.