How to compare two MutableArrays and display the unmatched value in iphone? [duplicate] - iphone

This question already has answers here:
Compare 2 nsmutablearray and get different object to third array in ios
(4 answers)
Closed 9 years ago.
I have two MutableArray values like.
NSMutableArray *array=[[NSMutableArray alloc]initWithObjects:#"Apple", #"Orange", #"Grapes", #"Banana", nil];
NSMutableArray *array1=[[NSMutableArray alloc]initWithObjects:#"Apple", #"Orange", #"Grapes", nil];
Now i have to compare that two Mutable arrays and display that unmatched object "Banana" into one string.
I am fresher to iOS so, anybody would send me the code for that problem.
Thanks in Advance.

As others have suggest, NSSet is probably your best bet. However, given that *array is mutable, you could simply remove the objects from it contained in *array1
NSMutableArray *array=[[NSMutableArray alloc]initWithObjects:#"Apple", #"Orange", #"Grapes", #"Banana", nil];
NSMutableArray *array1=[[NSMutableArray alloc]initWithObjects:#"Apple", #"Orange", #"Grapes", nil];
[array removeObjectsInArray:array1];
NSLog(#"array: %#", array); // array: ( Banana )
// if you require result as a string
NSString *objectsAsString = [array componentsJoinedByString:#", "];
NSLog(#"objects as string: %#", objectsAsString); // objects as string: Banana

for(int i=0;i<[array count];i++)
{
NSString *str1 = [array objectAtIndex:i];
for(int j=0;j<[array1 count];j++)
{
NSString *str2 = [array1 objectAtIndex:j];
if([str1 isEqualToString:str2])
{
//do something which you want i.e add the values to some other array
}
}
}

You should probably use NSSet for this purpose
NSSet *set1 = [NSSet setWithObjects:#"a", #"s", #"d", #"f", nil];
NSSet *set2 = [NSSet setWithObjects:#"a", #"s", nil];
NSMutableSet *notInSet1 = [NSMutableSet setWithSet:set2];
[notInSet1 minusSet:set1];
NSMutableSet *notInSet2 = [NSMutableSet setWithSet:set1];
[notInSet2 minusSet:set2];
NSMutableSet *symmetricDifference = [NSMutableSet setWithSet:notInSet1];
[symmetricDifference unionSet:notInSet2];

NSArray *array1 = [[NSArray alloc] initWithObjects:#"a",#"b",#"c",nil];
NSArray *array2 = [[NSArray alloc] initWithObjects:#"a",#"d",#"c",nil];
NSMutableArray *ary_result = [[NSMutableArray alloc] init];
NSMutableArray *ary_resultUnmatched = [[NSMutableArray alloc] init];
for(int i = 0;i<[array1 count];i++)
{
for(int j= 0;j<[array2 count];j++)
{
if([[array1 objectAtIndex:i] isEqualToString:[array2 objectAtIndex:j]])
{
[ary_result addObject:[array1 objectAtIndex:i]];
} else {
[ary_resultUnmatched addObject:[array1 objectAtIndex:i]];
}
}
}
NSLog(#"%#",ary_result);//it will print a,c
NSLog(#"%#",ary_resultUnmatched);//it will print b,d
so in else condition you'll have your un matched values

Related

How to join an NSArray output to an NSString separated with commas

I'm using the following code to try to join the array output into an NSString.
NSArray *array1 = [NSArray arrayWithObjects:[item objectForKey:#"id"], nil];
NSString *joinedString = [array1 componentsJoinedByString:#","];
NSLog(#"joinedString is %#", joinedString);
I would like this to output the joined string as: joined string is 55,56,57,66,88... etc... at the moment the output is:
2013-03-05 13:13:17.052 [63705:907] joinedString is 55
2013-03-05 13:13:17.056 [63705:907] joinedString is 56
2013-03-05 13:13:17.060 [63705:907] joinedString is 57
2013-03-05 13:13:17.064 [63705:907] joinedString is 66
You are probably running the join method inside a loop.
I suppose this is what you want.
NSMutableArray * array1 = [NSMutableArray array]; // create a Mutable array
for(id item in items){
[array1 addObject:[item objectForKey:#"id"]]; // Add the values to this created mutable array
}
NSString *joinedString = [array1 componentsJoinedByString:#","];
NSLog(#"joinedString is %#", joinedString);
You can do it as,
take for example
NSArray *array=#[#"A",#"B",#"C"];
NSString *string=[array componentsJoinedByString:#","];
NSLog(#"%#",string);
Output is :
A,B,C
What ever you are writing that one correct may be problem in [item objectForKey:#"id"] once check this one other all are fine.
NSMutableArray *array = [[NSMutableArray alloc]
initWithObjects:#"55",#"56",#"57",#"58", nil];
NSString *joinedString = [array componentsJoinedByString:#","];
NSLog(#"%#",joinedString);
I have been commenting on a couple of the answers here and found that most of the answers are just giving the code provided as the answer to solve this code, and the reason for that is because the code provided (See Provided code) works perfectly fine.
(Provide by question asker)
NSArray *array1 = [NSArray arrayWithObjects:[item objectForKey:#"id"], nil];
NSString *joinedString = [array1 componentsJoinedByString:#","];
NSLog(#"joinedString is %#", joinedString);
As the user hasn't provided how the item NSDictionary is created I am assuming that an NSArray has been created which contains some NSDictionaries
NSArray *array = [[NSArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:#"55", #"id", nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"65", #"id", nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"75", #"id", nil],
[NSDictionary dictionaryWithObjectsAndKeys:#"65", #"id", nil],
nil];
The problem is with the code that hasn't been provide, because we know that item is an NSDictionary we know that [item objectForKey:#"id"] doesn't return an individual items it returns an NSArray of ids. So based on if it was an NSArray it would log something like joinedString is (55, 56, 57...)". We also know that it can't just be a string as we would also only have one value than so it would log some thing like this joinedString is 55, and again this isn't what is wanted so. the only way to get what has been provided would be to have something like this
for(NSDictionary *item in array) {
NSArray *array1 = [NSArray arrayWithObjects:[item objectForKey:#"id"], nil];
NSString *joinedString = [array1 componentsJoinedByString:#","];
NSLog(#"joinedString is %#", joinedString);
}
So if this is the case than the way to resolve this would be to do
NSMutableArray *array1 = [NSMutableArray array];
for(NSDictionary *item in array) {
[array1 addObject:[item objectForKey:#"id"]];
}
// Note that this doesn't need to be in a for loop `componentsJoinedByString:` only needs to run once.
NSString *joinedString = [array1 componentsJoinedByString:#","];
NSLog(#"joinedString is %#", joinedString);
The output of this would be (As user wants)
joinedString is 55,65,75,65
Once the question asker provides the missing code I will correct his to answer based on there code but until then I am assuming.
EDIT:
First Check [item objectForKey:#"id"] it is proper or not ??
And Then use following code :
NSArray *array1 = [NSArray arrayWithObjects:[item objectForKey:#"id"], nil];
NSString *commaSpStr;
commaSpStr = [array1 componentsJoinedByString:#", "];
NSLog(#"%#", commaSpStr);
You are recreating array1 everytime. Create an instance variable of array1, insert [item objectForKey:#"id"] value to it and you will see joinedString will be updated.
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (NSDictionary *item in array) {
[arr addObject:[item objectForKey:#"id"]];
}
NSString *joinedStr = [arr componentsJoinedByString:#","];

Initialize multiple objects with different name

How can I Initialize and allocate multiple objects with different name and pass it to the NSArray. from Below code the object is initialized once in loop and I need to initialized multiple times as per the For loop will go with different name..and then pass it to NSArray.please check the code below..
when For loop will start means i=0 ..initialized item would betempItemi
now next time when i=1 and i=2 the tempItemi name will be same .how can i change this with in loop..and pass it to NSArray *items
for (int i = 0; i< [Array count]; i++)
{
id object = [Array objectAtIndex:i];
if ([object isKindOfClass:[NSDictionary class]])
{
NSDictionary *objDict = (NSDictionary *)object;
ECGraphItem *tempItemi = [[ECGraphItem alloc]init];
NSString *str = [objDict objectForKey:#"title"];
NSLog(#"str value%#",str);
float f=[str floatValue];
tempItemi.isPercentage=YES;
tempItemi.yValue=f;
tempItemi.width=30;
NSArray *items = [[NSArray alloc] initWithObjects:tempItemi,nil];
//in array need to pass all the initialized values
[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];
}
}
Why dont you just make the array mutable and then add the object each time like this:
NSMutableArray *items = [[NSMutableArray alloc] init];
// a mutable array means you can add objects to it!
for (int i = 0; i< [Array count]; i++)
{
id object = [Array objectAtIndex:i];
if ([object isKindOfClass:[NSDictionary class]])
{
NSDictionary *objDict = (NSDictionary *)object;
ECGraphItem *tempItemi = [[ECGraphItem alloc]init];
NSString *str = [objDict objectForKey:#"title"];
NSLog(#"str value%#",str);
float f=[str floatValue];
tempItemi.isPercentage=YES;
tempItemi.yValue=f;
tempItemi.width=30;
[items addObject: tempItemi];
//in array need to pass all the initialized values
}
}
[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];
Anyways items in your original code will be reinitializing each time and you are drawing a new histogram each time so your code won't work... This should work...
The code you have written is ok but,
NSArray *items will always contain only one item at each loop.
just declare that outside for loop as NSMutableArray,
and go with the same code you are using.
As you said you want to make variable dynamically as
ECGraphItem *tempItemi = [[ECGraphItem alloc]init];
here i will be changing in the loop,
You can create a NSDictionary with key/value as per with your tempItem1/2/3/4.... as key and save values by alloc/init.
Then instead of a variable tempItem32, you will be using [dict valueForKey:#"tempItem32"].
EDIT:
Check this example if this may come handy
NSMutableDictionary *dict=[NSMutableDictionary new];
for (int i=1; i<11; i++) {
NSString *string=[NSString stringWithFormat:#"string%d",i];
[dict setObject:[NSString stringWithFormat:#"%d", i*i] forKey:string];
}
NSLog(#"dict is %#",dict);
NSString *fetch=#"string5";
NSLog(#"val:%#, for:%#",[dict valueForKey:fetch],fetch);

Sum duplicate on NSMutableArray

I have a NSMutableArray with objects of type NSMutableDictionary,
the NSMutableDictionary contains 2 keys
-Airlines (string)
-Rating (integer)
I have an NSMutableArray with all the objects and what i need is to Sum the rating of all the airline companies repeated objects, an example:
Airline Rating
A 2
B 3
B 4
C 5
The end result array will be the A = 2, C = 5 and the Sum of B´s that is equal to 7.
My code so far:
for (int i = 0; i < arrayMealRating.count; ++i) {
NSMutableDictionary *item = [arrayMealRating objectAtIndex:i];
NSLog(#"item=%#",item);
for (int j = i+1; j < arrayMealRating.count; ++j)
{
if ([[item valueForKey:#"Airline"] isEqualToString:[arrayMealRating objectAtIndex:j]]){
NSMutableDictionary *item = [arrayMealRating objectAtIndex:j];
NSMutableDictionary *item1 = [arrayMealRating objectAtIndex:i];
NSInteger auxCount = [[item valueForKey:#"setMealRating"] integerValue] + [[item1 valueForKey:#"setMealRating"] integerValue];
NSMutableDictionary *aux = [NSMutableDictionary dictionaryWithObjectsAndKeys:[item valueForKey:#"Airline"], #"Airline"
,[NSString stringWithFormat:#"%d",auxCount], #"setMealRating"
,nil];
NSLog(#"aux=%#",aux);
[arrayMealRating replaceObjectAtIndex:i withObject:aux];
}
}
}
A bit messy i know but i dont know how to work with NSMutableDictionary, any help will be much appreciated, Thanks in Advance!
Incase you dont want to change how your storing the data, heres how you would do it using key-value coding. Heres the dirrect link to the documentation for #distinctUnionOfObjects and #sum.
// Get all the airline names with no duplicates using the KVC #distinctUnionOfObjects collection operator
NSArray *airlineNames = [arrayMealRating valueForKeyPath:#"#distinctUnionOfObjects.Airline"];
// Loop through all the airlines
for (NSString *airline in airlineNames) {
// Get an array of all the dictionaries for the current airline
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(Airline == %#)", airline];
NSArray *airlineMealRating = [arrayMealRating filteredArrayUsingPredicate:predicate];
// Get the sum of all the ratings using KVC #sum collection operator
NSNumber *rating = [airlineMealRating valueForKeyPath:#"#sum.Rating"];
NSLog(#"%#: %#", airline, rating);
}
This gives the following output
A: 2
B: 7
C: 5
I would suggest to redesign that entirely if that's at all possible.
Create a class Airline
#interface Airline : NSObject
#property (strong, nonatomic) NSString *name;
#property (strong, nonatomic) NSMutableArray *mealRatings;
- (void)addMealRating:(float)rating;
- (float)sumOfMealRatings;
#end
#implementation
- (id)initWithName:(NSString *)pName
{
self = [super init];
if (self)
{
self.name = pName;
self.mealRatings = [NSMutableArray array];
}
return self;
}
- (void)addMealRating:(float)rating
{
[self.mealRatings addObject:#(rating)];
}
- (float)sumOfRatings
{
float sum = 0;
for (NSNumber *rating in self.mealRatings)
{
sum += [rating floatValue];
}
}
#end
Then in your 'mainclass' you simply hold an NSArray with instances of your Airline objects. It might require you to change some of your existing code, but I think in the long run it saves you time and trouble. Perhaps you recognize later on, that you want to add additional properties to your Airlines. A dictionary is a cumbersome way to do that.
#try this
NSMutableArray *myArray = [[NSMutableArray alloc] initWithCapacity:4];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:[NSNumber numberWithInteger:2] forKey:#"A"];
[myArray addObject:dict];
NSMutableDictionary *dict2 = [[NSMutableDictionary alloc] init];
[dict2 setValue:[NSNumber numberWithInteger:3] forKey:#"B"];
[myArray addObject:dict2];
NSMutableDictionary *dict3 = [[NSMutableDictionary alloc] init];
[dict3 setValue:[NSNumber numberWithInteger:4] forKey:#"B"];
[myArray addObject:dict3];
NSMutableDictionary *dict4 = [[NSMutableDictionary alloc] init];
[dict4 setValue:[NSNumber numberWithInteger:5] forKey:#"D"];
[myArray addObject:dict4];
NSMutableDictionary *resultDictionary = [[NSMutableDictionary alloc] init];
for(NSMutableDictionary *dictionary in myArray)
{
NSString *key = [[dictionary allKeys] objectAtIndex:0];
NSInteger previousValue = [[resultDictionary objectForKey:key] integerValue];
NSInteger value = [[dictionary objectForKey:key] integerValue];
previousValue += value;
[resultDictionary setObject:[NSNumber numberWithInteger:previousValue] forKey:key];
}
for(NSString *key in resultDictionary)
{
NSLog(#"value for key = %# = %d",key, [[resultDictionary valueForKey:key] integerValue]);
}

returning 8 closest cgfloat from a table lookup based on a cgfloat

I am trying to create this method. Let's call this
-(NSMutableArray*) getEightClosestSwatchesFor:(CGFloat)hue
{
NSString *myFile = [[NSBundle mainBundle] pathForResource:#"festival101" ofType:#"plist"];
NSMutableArray* myArray = [NSArray arrayWithContentsOfFile:myFile];
for (NSDictionary *dict in myArray)
{
NSLog(#"[plistData valueForKey:aKey] string] is %f", [[dict valueForKey:#"hue"] floatValue]) ;
}
return myArray;
}
pretty much, I am passing a cgfloat to this method which then needs to check a plist file which have hue key for 100 elements. I need to compare my hue with all of the hues and get 8 most closest hue and finally wrap these into an array and return this.
What would be most efficient way of doing this? Thanks in advance.
Here's my method if anyone is interested. Feel free to comment on it.
-(NSArray*)eightClosestSwatchesForHue:(CGFloat)hue
{
NSMutableArray *updatedArray = [[NSMutableArray alloc] initWithCapacity:100];
NSString *myFile = [[NSBundle mainBundle] pathForResource:#"festival101" ofType:#"plist"];
NSMutableArray* myArray = [NSArray arrayWithContentsOfFile:myFile];
for (NSDictionary *dict in myArray)
{
CGFloat differenceHue = fabs(hue - [[dict valueForKey:#"hue"] floatValue]);
//create a KVA for the differenceHue here and then add it to the dictionary and add this dictionary to the array.
NSDictionary* tempDict = [NSDictionary dictionaryWithObjectsAndKeys:
[dict valueForKey:#"id"], #"id",
[NSNumber numberWithFloat:differenceHue], #"differenceHue",
[dict valueForKey:#"color"], #"color",
nil];
[updatedArray addObject:tempDict];
}
//now we have an array of dictioneries with values we want. we need to sort this from little to big now.
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"differenceHue" ascending:YES];
[updatedArray sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
[descriptor release];
//now get the first 8 elements and get rid of the remaining.
NSArray *finalArray = [updatedArray subarrayWithRange:NSMakeRange(0,8)];
[updatedArray release];
return finalArray;
}

Multidimensional Arrays

im trying to populate a UITable with XML, i already have the xml parsed and stored in an array
ex.
array [item0, item1, item2, item3, item4, item5, item6, item7]
i need help trying to convert the array into an array that has 2 columns
ex
array [[item0, item1], [item2, item3], [item4, item5], [item6, item7]]
any help would be greatly appreciated
thanks
You could use something like this.
NSMutableArray *rootArray = [NSMutableArray array];
for (NSInteger i = 1; i < [items count]; i+=2) {
id object1 = [items objectAtIndex:i-1];
id object2 = [items objectAtIndex:i];
[rootArray addObject:[NSArray arrayWithObjects:object1, object2, nil]];
}
This will ignore the last object if you have a uneven number of objects in your array.
Edit, the version that doesn't ignore the last single object.
NSMutableArray *rootArray = [NSMutableArray array];
for (NSInteger i = 0; i < [items count]; i += 2) {
id object1 = [items objectAtIndex:i];
id object2 = nil;
if (i+1 < [items count]) {
object2 = [items objectAtIndex:i+1];
}
[rootArray addObject:[NSArray arrayWithObjects:object1, object2, nil]];
}
make structure some thing like this
NSMutableArray *dictArray=[NSMutableArray alloc] init];
for(int i=0;i<[array count];i=i+2)
{
NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
[dict setObject:[array objectAtIndex:i] forKey:#"first"];
if([array count]>(i+1))
[dict setObject:[array objectAtIndex:(i+1)] forKey:#"second"];
[dictArray addObject:dict];
[dict release];
}
[dictArray release];