Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate odd and even indexes in a sequence array

Tags:

arrays

swift

A for in loop won't work because type int is does not conform to protocol Sequence.

I have 3 arrays:

1 master array, where the array is stored

1 odd array, empty at the beginning

1 even array, empty at the beginning

The meaning is that all the odd indexes of the master array will be stored at the odd array, and the same for the even array.

    while i < masterA.count {
        evenA.append(masterA[i])
        if i > 0{
        oddA.append(masterA[i - 1])
        }
        i += 2
    }

However this does not work good enough. Anyone has a better idea?

like image 920
Petravd1994 Avatar asked Oct 11 '16 18:10

Petravd1994


People also ask

How do I extract even elements of an array?

To extract elements with an even index from an array., we can use the array filter instance method. to call filter with a callback that has index as the 2nd parameter. We return index % 2 === 0 to filter out all the elements that have odd indexes. Therefore, filtered is [4, 5, 7, 8, 14, 45, 76] .

How do you separate odd and even numbers in Javascript?

log(seperateArray(numbers)); This works by using the modulo '%' sign which returns the remainder after dividing a number. So in this case, all odd numbers divided by 2 would return a remainder of 1 and all even numbers would return 0.


1 Answers

Here is another possible solution:

let evenA = stride(from: 0, to: masterA.count, by: 2).map { masterA[$0] }
let oddA = stride(from: 1, to: masterA.count, by: 2).map { masterA[$0] }

The elements are "picked" directly from the even/odd positions in the source array.

Performance comparison:

My simple, not very sophisticated ad-hoc benchmarking code:

import Swift

let N = 10_000_000
let RUNS = 50

let masterA = (0..<N).map { $0 }

var times = (0.0, 0.0, 0.0, 0.0)
for _ in 1...RUNS {

    // filter+map (dfri)
    do {
        let start = Date()
        let evenA = masterA.enumerated().filter { $0.0 % 2 == 0 }.map{ $0.1 }
        let oddA = masterA.enumerated().filter { $0.0 % 2 != 0 }.map{ $0.1 }
        let time = Date().timeIntervalSince(start)
        times.0 += time
    }

    // flatMap (dfri)
    do {
        let start = Date()
        let evenA = masterA.enumerated().flatMap { $0 % 2 == 0 ? $1 : nil }
        let oddA = masterA.enumerated().flatMap { $0 % 2 != 0 ? $1 : nil }
        let time = Date().timeIntervalSince(start)
        times.1 += time
    }

    // stride+map (me)
    do {
        let start = Date()
        let evenA = stride(from: 0, to: masterA.count, by: 2).map { masterA[$0] }
        let oddA = stride(from: 1, to: masterA.count, by: 2).map { masterA[$0] }
        let time = Date().timeIntervalSince(start)
        times.2 += time
    }

    // loop (Keiwan)
    do {
        let start = Date()
        var evenA = [Int]()
        var oddA = [Int]()

        for (index, element) in masterA.enumerated() {
            if index % 2 == 0 {
                evenA.append(element)
            } else {
                oddA.append(element)
            }
        }
        let time = Date().timeIntervalSince(start)
        times.3 += time
    }
}

print(N, RUNS)
print(times.0/Double(RUNS), times.1/Double(RUNS), times.2/Double(RUNS), times.3/Double(RUNS))

Results: (On a MacBook, running in Release mode)

#elements   filter+map  flatMap   stride+map  loop
10,000      0.0001      0.00008   0.00004     0.00004
100,000     0.0016      0.0008    0.0004      0.0004
1,000,000   0.0295      0.0136    0.0090      0.0091
10,000,000  0.3025      0.1332    0.0909      0.1250
like image 104
Martin R Avatar answered Oct 01 '22 08:10

Martin R