0

I have some struct

struct WheelBrand {
var id : String?
var name : String?

mutating func removeAll() {
    self = WheelBrand()
}

I appended it :

for (_,subJson):(String, JSON) in json["data"]{
  let id = subJson["make_id"].stringValue
  let name = subJson["make"].stringValue
  self.itemWheelBrand.append(WheelBrand(id: id, name: name))
  }

How can i remove duplicates from my struct, or i have to append it with unique values?

1 Answers1

0

You can filter this way after you finish appending to the array.

    var unique = [WheelBrand]()
    for arrValue in itemWheelBrand {
        if !unique.contains(where: { $0.id == arrValue.id }) {
            unique.append(arrValue)
        }
    }
Hilalkah
  • 945
  • 15
  • 37
  • 1
    2 problems with this: 1) Using an array for uniqueness checking causes this algorithm to be `O(n^2)`, whereas a set would be `O(n)` 2) This misses a perfect chance to use `filter` or `removeAll`. – Alexander Jun 25 '19 at 14:17