ios - What is the best way to get unique set of objects from an array in objective C -
i have array list of model objects
model object has properties name, age, gender, have date follows
user* user1.name = "bob", user1.age = "10", user1.gender = "m", user* user2.name = "bob", user2.age = "11", user2.gender = "m", user* user3.name = "woz", user3.age = "15", user3.gender = "m", user* user4.name = "woz", user4.age = "16", user4.gender = "m",
now need unique name , lesser age result.
user* user1.name = "bob", user1.age = "10", user1.gender = "m", user* user3.name = "woz", user3.age = "15", user3.gender = "m",
what efficient way derive above result. in advance hint or appreciated.
you can iterate array looking youngest users, storing each 1 in dictionary keyed name. once have iterated array of dictionary values array;
-(nsarray*)getyoungestuniqueusersfromarray:(nsarray*)users { nsmutabledictionary* youngestusersdict = [nsmutabledictionary new]; (user* user in users) { user* currentuser = youngestusersdict[user.name]; if (currentuser == nil || user.age < currentuser.age) { youngestusersdict[user.name]=user; } } nsarray* youngestusers = [youngestusersdict allvalues]; return youngestusers; }