0

I have an array of contacts. These contacts have an NSSet of Phone objects and each phone object has a phoneNumber string. I want to display all these contacts, but I don't want contacts with duplicate numbers to show up. The issue now is some contacts have duplicate numbers, but may also have an additional number. In other words, one user may have 555-555-5555 and another may have 555-555-5555 plus 666-666-6666. How can I add just the contact with the two numbers to the new contacts array? Is there a way to filter the array with NSPredicate or any other elegant way or do I have to just use a bunch of for loops?

I know I could just put the phone numbers into an NSSet, but I also want to be able to display the contact name, which is in the Contact object.

To put it more simply, I want to be able to filter out any subsets, or just take the largest set containing that phone.

mergesort
  • 5,087
  • 13
  • 38
  • 63

1 Answers1

0

You should be able to do something like this: https://stackoverflow.com/a/7491851/1009087.

Iterate over the array of all contacts, adding values to an NSMutableSet. If the value already exists in the set, remove it from your array of contacts.

You have to iterate over the whole list of contacts but each step is constant time. Just set your identifier to be the combination of properties that make each contact unique, name and phone number.

Community
  • 1
  • 1
Civatrix
  • 2,566
  • 4
  • 17
  • 16