42
let items: [String] = ["A", "B", "A", "C", "A", "D"]

items.whatFunction("A") // -> [0, 2, 4]
items.whatFunction("B") // -> [1]

Does Swift 3 support a function like whatFunction(_: Element)?

If not, what is the most efficient logic?

3

9 Answers 9

51

You can filter the indices of the array directly, it avoids the extra mapping.

let items = ["A", "B", "A", "C", "A", "D"]
let filteredIndices = items.indices.filter {items[$0] == "A"}

or as Array extension:

extension Array where Element: Equatable {

    func whatFunction(_ value :  Element) -> [Int] {
        return self.indices.filter {self[$0] == value}
    }

}

items.whatFunction("A") // -> [0, 2, 4]
items.whatFunction("B") // -> [1]

or still more generic

extension Collection where Element: Equatable {

    func whatFunction(_ value :  Element) -> [Index] {
        return self.indices.filter {self[$0] == value}
    }

}
2
36

You can create your own extension for arrays.

extension Array where Element: Equatable {
    func indexes(of element: Element) -> [Int] {
        return self.enumerated().filter({ element == $0.element }).map({ $0.offset })
    }
}

You can simply call it like this

items.indexes(of: "A") // [0, 2, 4]
items.indexes(of: "B") // [1]
1
  • using .enumerated() is a bad idea since its not guaranteed to be the index as stated by the apple docu: "When you enumerate a collection, the integer part of each pair is a counter for the enumeration, but is not necessarily the index of the paired value."
    – zero3nna
    Commented Apr 7, 2023 at 19:22
33

You can achieve this by chain of:

  1. enumerated() - add indexes;
  2. filter() out unnecessary items;
  3. map() our indexes.

Example (works in Swift 3 - Swift 4.x):

let items: [String] = ["A", "B", "A", "C", "A", "D"]  
print(items.enumerated().filter({ $0.element == "A" }).map({ $0.offset })) // -> [0, 2, 4]

Another way is using flatMap, which allows you to check the element and return index if needed in one closure.

Example (works in Swift 3 - Swift 4.0):

print(items.enumerated().flatMap { $0.element == "A" ? $0.offset : nil }) // -> [0, 2, 4]

But since Swift 4.1 flatMap that can return non-nil objects become deprecated and instead you should use compactMap.

Example (works since Swift 4.1):

print(items.enumerated().compactMap { $0.element == "A" ? $0.offset : nil }) // -> [0, 2, 4]

And the cleanest and the most memory-cheap way is to iterate through array indices and check if element of array at current index equals to required element.

Example (works in Swift 3 - Swift 5.x):

print(items.indices.filter({ items[$0] == "A" })) // -> [0, 2, 4]
3
  • 1
    Note that you could also combine filter and map into flatMap, as Julien did, but more details in the answer is good too ;) Commented Aug 29, 2017 at 8:22
  • Just another approach is all. Personally I usually prefer to split filter and map into separate steps if they have different basis, as this does. If on the other hand one wanted to filter and map on the same property, flatMap makes more sense. Commented Aug 31, 2017 at 6:10
  • using .enumerated() is a bad idea since its not guaranteed to be the index as stated by the apple docu: "When you enumerate a collection, the integer part of each pair is a counter for the enumeration, but is not necessarily the index of the paired value."
    – zero3nna
    Commented Apr 7, 2023 at 19:19
9

In Swift 3 and Swift 4 you can do that:

let items: [String] = ["A", "B", "A", "C", "A", "D"]

extension Array where Element: Equatable {

    func indexes(of item: Element) -> [Int]  {
        return enumerated().compactMap { $0.element == item ? $0.offset : nil }
    }
}

items.indexes(of: "A")

I hope my answer was helpful 😊

0
4

you can use it like that :

 let items: [String] = ["A", "B", "A", "C", "A", "D"]

        let indexes = items.enumerated().filter {
            $0.element == "A"
            }.map{$0.offset}

        print(indexes)
1

just copy and paste

extension Array {
  func whatFunction(_ ids :  String) -> [Int] {

    var mutableArr = [Int]()
    for i in 0..<self.count {
        if ((self[i] as! String) == ids) {
            mutableArr.append(i)
        }
    }
        return mutableArr 
  }

}
1

You can use that below code:

var firstArray = ["k","d","r","r","p","k","b","p","k","k"]
var secondArray = ["k","d","r","s","d","r","b","c"]

let filterArray = firstArray.filter { secondArray.contains($0) }
let filterArray1 = firstArray.filter { !secondArray.contains($0) }
let filterIndex = firstArray.enumerated().filter { $0.element == "k" }.map { $0.offset }
print(filterArray) --> // ["k", "d", "r", "r", "k", "b", "k", "k"]
print(filterArray1) --> // ["p", "p"]
print(filterIndex) --> // [0, 5, 8, 9]
0

this can be a way too

// MARK: - ZIP: Dictionary like

let words = ["One", "Two", "Three", "Four"]
let numbers = 1...words.count

for (word, number) in zip(words, numbers) {
    print("\n\(word): \(number)")
}
-1

For example finding the indices of p_last values that are in inds1 array: (swift 4+)

let p_last = [51,42]
let inds1 = [1,3,51,42,4]
let idx1 = Array(inds1.filter{ p_last.contains($0) }.indices)

idx1 = [0,1]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.