ios - Merging two NSArray removing duplicates -
i have array of cities , array of countries. these 2 objects have property in common: city name. need create new array adding countries have city name not there in cities array.
i tried iterating on both arrays, comparing city name , adding different 1 new array. result that, while checks first city, cities have name added array , other cities in cities array there.
self.filteredcountriesarray = [nsmutablearray new]; (country* country in self.countries) { (city *city in self.cities) { if (![country.city isequaltostring:city.name]) { [self.filteredcountriesarray addobject:country]; } } }
suggestions?
not sure whether understand want, guess you'd this:
for (country *country in self.countries) { bool found = no; (city *city in self.cities) { if ([country.city isequaltostring:city.name]) { found = yes; break; } } if (!found) { [self.filteredcountriesarray addobject:country]; } }
to speed up, i'd create nsset
city names first:
nsmutableset *citynames = [nsmutableset set]; (city *city in self.cities) { [citynames addobject:city.name]; } (country *country in self.countries) { if (![citynames containsobject:county.city]) { [self.filteredcountriesarray addobject:country]; } }
Comments
Post a Comment