How to remove common items from two struct arrays in Swift

前端 未结 3 1492
旧时难觅i
旧时难觅i 2021-01-16 15:39

In my app I have two struct arrays and I want to remove common items from one of them. My struct:

struct PeopleSelectItem {
    var name = \"\"
    var id =          


        
3条回答
  •  無奈伤痛
    2021-01-16 16:12

    If people might have a significant amount of entries, performance should be considered. So, instead of searching with an n^2 algorithm, you should make use of Swifts dictionary and the corresponding hash-search to find items.

    If Id is unique for people then I would store them in a dictionary like:

        var peopleDict: [String:PeopleSelectItem] = [:]()
    

    You can easily convert from the array you have to this dictionary:

        people.foreach {peopleDict[$0.id] = $0}
    

    With this dictionary it's very easy to delete single entries:

        selectedPeople.foreach {peopleDict.remove($0.id)}
    

    Optionally to switch back to an array for people you just say:

        let filteredPeople = peopleDict.values as [PeopleSelectItem]
    

    Remarks

    1. I assumed, that selectedPeople is smaller then the base of all people. If this is not the case, you should pu selectedPeople in a dictionary.
    2. did I say I like this Spark like api? I think I do so.
    3. I just wrote that code from top of my mind. If it is not completely syntactically correct let me know and I correct it.

提交回复
热议问题