Swift: second occurrence with indexOf

  • List item

You can perform another search for the index of element at the remaining array slice as follow:

edit/update: Swift 5.2 or later

extension Collection where Element: Equatable {
    /// Returns the second index where the specified value appears in the collection.
    func secondIndex(of element: Element) -> Index? {
        guard let index = firstIndex(of: element) else { return nil }
        return self[self.index(after: index)...].firstIndex(of: element)
    }
}

extension Collection {
    /// Returns the second index in which an element of the collection satisfies the given predicate.
    func secondIndex(where predicate: (Element) throws -> Bool) rethrows -> Index? {
        guard let index = try firstIndex(where: predicate) else { return nil }
        return try self[self.index(after: index)...].firstIndex(where: predicate)
    }
}

Testing:

let numbers = [1,3,4,5,5,9,0,1]
if let index = numbers.secondIndex(of: 5) {
    print(index)    // "4\n"
} else {
    print("not found")
}    
if let index = numbers.secondIndex(where: { $0.isMultiple(of: 3) }) {
    print(index)    // "5\n"
} else {
    print("not found")
}

I don't think you can do it with indexOf. Instead you'll have to use a for-loop. A shorthand version:

let numbers = [1,3,4,5,5,9,0,1]
var indexes = [Int]()
numbers.enumerate().forEach { if $0.element == 5 { indexes += [$0.index] } }

print(indexes) // [3, 4]

Here's a general use extension of Array that will work for finding the nth element of a kind in any array:

extension Array where Element: Equatable {
    // returns nil if there is no nth occurence
    // or the index of the nth occurence if there is
    func findNthIndexOf(n: Int, thing: Element) -> Int? {
        guard n > 0 else { return nil }
        var count = 0
        for (index, item) in enumerate() where item == thing {
            count += 1
            if count == n {
                return index
            }
        }
        return nil
    }
}

let numbers = [1,3,4,5,5,9,0]

numbers.findNthIndexOf(2, thing: 5) // returns 4