Swift Language Arrays Comparing 2 Arrays with zip

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!


The zip function accepts 2 parameters of type SequenceType and returns a Zip2Sequence where each element contains a value from the first sequence and one from the second sequence.


let nums = [1, 2, 3]
let animals = ["Dog", "Cat", "Tiger"]
let numsAndAnimals = zip(nums, animals)

nomsAndAnimals now contains the following values


This is useful when you want to perform some kind of comparation between the n-th element of each Array.


Given 2 Arrays of Int(s)

let list0 = [0, 2, 4]
let list1 = [0, 4, 8]

you want to check whether each value into list1 is the double of the related value in list0.

let list1HasDoubleOfList0 = !zip(list0, list1).filter { $0 != (2 * $1)}.isEmpty

Got any Swift Language Question?