I have a model object called car which has following attributes
- Name
- Color
- Type
In an array(Name: oldArray) I have several car objects. In a common Interval I will hit an API and get an another array (Name: newArray) of several car object. I need to compare two arrays and get the list of unique Items and show it to user.
Conditions. 1. If the object from newArray is not in oldArray then I need to Inform user that he has a new car along with the car name 2. If the object from oldArray is not in newArray then I need to Inform User that he has sold his car along with the car name
I have created the following method
- (NSMutableArray *)getModifiedUserCarsFrom:(NSMutableArray *)savedCars NewCars:(NSMutableArray *)newCars {
NSMutableArray *loopArray1;
NSMutableArray *loopArray2;
NSMutableArray *modifiedCars = [NSMutableArray array];
if (newCars.count >= savedCars.count) {
loopArray1 = newCars;
loopArray2 = savedCars;
} else {
loopArray1 = savedCars;
loopArray2 = newCars;
}
for (Car *old in loopArray1) {
BOOL isCarExist = NO;
for (Car *new in loopArray2) {
if ([new.name isEqualToString:old.name]) {
isCarExist = YES;
break;
}
}
if (!isCarExist) {
[modifiedCars addObject:olde];
}
}
return modifiedCars;
}
Is there any better and Faster method than this? Any comments and suggestions are Appreciated.