2

I am getting a json data and appending it to a tuple there are duplicates in this array is there a way i can remove them? here is how am getting the json data and setting it to the tupple

 if let hru = ($0["Menu"]["title"]).string{
     let uw = ($0["name"]).string
     let tagloc = (tag: hru, location: uw)
     self.resultTuples.append(tagloc)
   }

Am printing the tuple like this

for var i = 0; i < self.resultTuples.count; ++i{
      print(self.resultTuples[i])
 }

But what gets printed is

tag: Rice Dishes
tag: Rice Dishes
tag: Cakes and Creams
tag: Cakes and Creams
tag: Cakes and Creams
tag: Pastries and Others
tag: Pastries and Others
tag: Pastries and Others
tag: Pastries and Others
tag: Pastries and Others
tag: Pastries and Others
tag: Pastries and Others
tag: Pastries and Others
tag: Pastries and Others
tag: Pastries and Others
tag: Soups and Sauces
tag: Soups and Sauces
....

I want to remove all the duplicates from this tuple

EDIT:

Using array did not work i have a model Menus

if let hru = ($0["Menu"]["title"]).string{

     var men = Menus(nam: hru)
      let set = NSSet(array: self.menus)
     self.menus = set.allObjects as! [Menus]
      self.menus.append(men)

    }

     for i in self.menus{
            print("MENUSS \(i.name)")

      }
lordxx
  • 25
  • 5
  • Possible duplicate of [Remove duplicate objects in an array](http://stackoverflow.com/questions/34709066/remove-duplicate-objects-in-an-array) – dfrib Sep 21 '16 at 08:15
  • This is a tuple – lordxx Sep 21 '16 at 08:22
  • I just checked :/ sadly tuple is not an object, does not work with AnyObject, so you cant convert it to Set or NSSet, i think tuples has quite the limited function, so i suggest you change to use Struct or Class then it will be better, refactor it to Struct as below answer provided – Tj3n Sep 21 '16 at 08:40
  • You have an array of tuples (`resultTuples`?), so the technique is the same. – dfrib Sep 21 '16 at 08:40
  • Ok @Tj3n i think i will a normal array – lordxx Sep 21 '16 at 08:45

2 Answers2

4

If you use a model value like a struct instead of a tuple

struct TagAndLocation: Hashable {

    let tag: String
    let location: String

    var hashValue: Int { return tag.hashValue }
}

func ==(left:TagAndLocation, right: TagAndLocation) -> Bool {
    return left.tag == right.tag && left.location == right.location
}

You can leverage the Set functionalities to remove duplicates

let results: [TagAndLocation] = ...
let uniqueResults = Array(Set(results))

Please note that in this case you lose the original sort order.

Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148
0

You could check if a tuple is contained in a specific array before the insertion with the following function:

func containsTuple(arr: [(String, String)], tup:(String, String)) -> Bool {    
     let (c1, c2) = tup

     for (v1, v2) in arr {
        if v1 == c1 && v2 == c2 {
            return true
        }
     }

    return false
}

More info here How do I check if an array of tuples contains a particular one in Swift?

Community
  • 1
  • 1
g0tcha-
  • 323
  • 3
  • 5