-2

Currently using the following to find a match within an array, but I want to know if there is a more efficient way of doing such a check.

for player in team.roster {
     if id == player.id {
          return player.name
     }
}

Basically taking a variable that is given, a player id, and looping through a team roster to find a matching id, which given the size of the team is okay, but in the event a similar check is done on a larger data set what would be a better way to handle this?

4
  • use default property team.roster.first{where: ..... Commented Jan 13, 2022 at 4:49
  • https://developer.apple.com/documentation/swift/array/1848165-first Commented Jan 13, 2022 at 5:12
  • If the entries are sorted by their id then you can do a binary search. Or you can maintain an index. Otherwise you'll always have to traverse the array to find a matching entry. Using first(where:) does not make it more efficient. Commented Jan 13, 2022 at 5:42
  • To clarify, by "maintain an index", Martin is talking about building a dictionary that maps IDs to players. You pay some up-front cost, but subsequent repeated look-ups become faster. Commented Jan 13, 2022 at 6:24

3 Answers 3

0

Use the default property first{ condition }

    let id = 4
    
    let roster: [TeamMember] = [.init(id: 1, name: "Abishek", age: 19),
                               .init(id: 2, name: "Dinesh", age: 22),
                               .init(id: 3, name: "Praveen", age: 24),
                               .init(id: 4, name: "Sam", age: 25),
                               .init(id: 5, name: "David", age: 21)]
let firstMember = roster.first{$0.id == id}

print(firstMember)
print(firstMember?.name)

Output

Optional(TeamMember(id: 4, name: "Sam", age: 25.0))
Optional("Sam")
Sign up to request clarification or add additional context in comments.

1 Comment

This is tidier, but equivalent, performance-wise.
0

@Shabnam Siddiqui answer is Swifty and I would use it most of the time, but the binary search is worth to know as well.

The Swift method first(where:) in O(n)complexity https://developer.apple.com/documentation/swift/array/1848165-first, whereas a binary search has a worst-case performance of O(log n)‎ and a best-case performance of O(1)‎.

So assuming :

struct Player {
var id : Int
var name : String
}

for :

var players : [Player] = [
    Player(id: 0, name: "Bob"),
    Player(id: 1, name: "Edd"),
    Player(id: 2, name: "Jo"),
    Player(id: 3, name: "John")
]

Solution recursive :

func getPlayername(_ id : Int,  _ players : [Player]) -> String?{
    guard players.count > 0 else {
     print("No players found with id \(id)")
     return nil
    }
    if players.first!.id == id {
        return players.first?.name
    }else{
        return getPlayername(id, Array(players.dropFirst()))
    }
}
print(getPlayername(3, players))

Solution binary :

func getPlayerBinary(_ id : Int,  _ players : [Player]) -> String?{
    var startIndex = 0
    var max = players.count

    while startIndex < max {
      let midIndex = startIndex + (max - startIndex) / 2
        if players[midIndex].id == id {
            return players[midIndex].name
        } else if players[midIndex].id < id {
        lowerBound = midIndex + 1
      } else {
        max = midIndex
      }
    }
    print("No players found with id \(id)")
    return nil
}

https://www.geeksforgeeks.org/binary-search/

4 Comments

That is not a binary search.
Right right, Editing the binary option now :)
If you're going to spend the time to implement binary search, I'd opt to at least make it generic, so it has more reuse value
Various implementations here: stackoverflow.com/q/31904396/1187415.
-1
let name = team.roster.first(where: { $0["player_id"] == id}).map{ $0["player_name"] }

2 Comments

Why should this be more efficient?
I did think of this, but wasn't sure how this is more efficient like @MartinR is asking. Would this not do the same thing, just with less lines of code?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.