0

I have an array of arrays like

var arr = [[("aa",1),("b",2)],[("c",3),("dd",4)],[("e",5),("f",6)]]

I want to find first index of dd in arr. I am trying this code but failed to get result

let val = arr.first(where: {$0.1 == "dd" })
0

5 Answers 5

3

You used multi-dimensional arrays, please clarify which index do you looking for(inner/outer index).

In case you mean outer array index:

let outerIndex = arr.firstIndex(where: { $0.contains(where: { $0.0 == "dd"}) })

In case you mean inner array index:

let innerIndex = arr.map({ $0.firstIndex(where: { $0.0 == "dd" }) }).filter({ $0 != nil }).first

In case you need both indexes:

var bothIndex: (outer: Int?, inner: Int?) {
    guard let outerIndex: Int = arr.firstIndex(where: { $0.contains(where: { $0.0 == "dd"}) }) else {
        assertionFailure()
        return (nil,nil)
    }
    
    let innerIndex: Int? = arr[outerIndex].firstIndex(where: { $0.0 == "dd"})
    return (outerIndex, innerIndex)
}
Sign up to request clarification or add additional context in comments.

Comments

2

First of all tuples are indexed from 0 so it should be $0.0 and to find an index you need to use firstIndex and lastly, since you have an array of arrays you need to drill down one level and then return when you have a match in an inner array, I use contains for the inner arrays

let val = arr.firstIndex(where: { 
    $0.contains(where: { tuple in tuple.0 == "dd" })
})

1 Comment

great answer, I was noodling around with the question and thought almost got it, but I think I was far from this answer :]
1

From the your code, you have an value to each letter like "aa" has 1, "b" has 2, and so on.

If you want to find the value corresponding to "dd", I would suggest you to use Hashmaps/Dictionary !!

So your code will be

   var values = ["aa":1, "b":2, "c":3, "dd":4, "e":5, "f":6]
   let val = values["dd"] //val = 4

4 Comments

While using a dictionary instead of an array of tuples certainly is a good solution you are missing the point that OP has an array of arrays with tuples so you loose the inner array with this answer.
what do you mean by OP ?? Sorry, I answered this one because I thought it would be way easier by using dictionary
OP is an acronym often used online to refer to the guy who asked the question or started a discussion thread, OP = Original Poster. As I mentioned, here a dictionary doesn’t really work since it is an array of arrays and therefore the data is grouped together.
ohhh!! my bad , sorry and Thanks @JoakimDanielson!!
1

val will be in this case the second inner array. The first inner array that contains "dd"

let val = arr.first(where: { $0.first(where: { $0.0 == "dd" }) != nil } )

Comments

0

You have an array of arrays. You would need the index of the outer as well as the inner array:


var arr = [[("aa",1),("b",2)],[("c",3),("dd",4)],[("e",5),("f",6)]]

let predicate: ((String,Int)) -> Bool = { $0.0 == "dd" }
if let index = arr.firstIndex(where: {$0.contains(where: predicate)}),
    let subIndex = arr[index].firstIndex(where: predicate) {
    print(arr[index][subIndex])  // ("dd", 4)
}

Expanding on that subject:

extension Collection where Element: Collection {
    func firstIndexAndSubIndex(where predicate: (Element.Element) -> Bool) -> (index: Index, subIndex: Element.Index)? {
        if let index = firstIndex(where: {$0.contains(where: predicate)}),
            let subIndex = self[index].firstIndex(where: predicate) {
            return (index,subIndex)
        }
        return nil
    }
}

usage:

var arr = [[("aa",1),("b",2)],[("c",3),("dd",4)],[("e",5),("f",6)]]

if let idxs = arr.firstIndexAndSubIndex(where: { $0.0 == "dd" } ) {
    print(arr[idxs.index][idxs.subIndex])  // ("dd", 4)
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.