GuiSoySauce
GuiSoySauce

Reputation: 1783

Get first and last values that are not zeros from array in Swift

I have the following array that is generated dynamically with integers. So I do not know it's values or how many values it has.

var myArray = [0,0,5001,0,5002,5003,0,5004,0,0]

And I am trying to get the first value that is not zero and the last value that is not zero. In this case:

first value = 5001 last value = 5004

Is there a way to do that without using a for loop? Or maybe by creating an extension for the array class?

If there's any answer here on stack for that please let me know and I will remove the question. Haven't found anything in here yet.

Upvotes: 0

Views: 1376

Answers (4)

Leo Dabus
Leo Dabus

Reputation: 236420

extension Array where Element: IntegerType {
    var firstPositive: Element? {
        for number in self {
            if number > 0 {
                return number
            }
        }
        return nil
    }
    var lastPositive: Element? {
        for number in reverse() {
            if number > 0 {
                return number
            }
        }
        return nil
    }

}
let myArray = [0,0,5001,0,5002,5003,0,5004,0,0]
myArray.firstPositive   // 5001
myArray.lastPositive   // 5004

Upvotes: 2

Duncan C
Duncan C

Reputation: 131436

As the others have pointed out, you can use a filter statement to accomplish this.

That means you don't have to write a for loop, but the filter statement still has to loop through every item in the loop.

Worse, the filter statement creates a new array. That's a needless allocation. Furthermore, the filter statement iterates through every item in the array. With a couple of loops that break when they find the first non-zero value you would be able to accomplish the same thing without iterating through anything other than the outer zeros. From each direction you'd stop at the first non-zero value. For a large array that becomes computationally significant.

Upvotes: 1

matt
matt

Reputation: 535536

Like this:

let myArray = [0,0,5001,0,5002,5003,0,5004,0,0]
let noZeros = myArray.filter{$0 != 0}
let firstValue = noZeros.first
let lastValue = noZeros.last

But notice that there is a loop hidden in that code; you can't possibly do this without a loop somewhere. The only way to do this is to look at every value in the array, and that implies a loop.

Upvotes: 2

dfrib
dfrib

Reputation: 73196

var myArray = [0,0,5001,0,5002,5003,0,5004,0,0]
let filteredArray = myArray.filter { $0 != 0 }

print("First value = \(filteredArray.first ?? 0)")
print("Last value = \(filteredArray.last ?? 0)")

/*   First value = 5001
     Last value = 5004   */

Upvotes: 2

Related Questions