how to compare 2 NSMutableArray and get different number of value - objective-c

I want compare 2 NSMutableArray values and check which indexPath is different and store this numbers of index in one array.
for example I have these NSMutableArray :
NSMutableArray *a = [[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6", nil];
NSMutableArray *b = [[NSMutableArray alloc]initWithObjects:#"11",#"2",#"5",#"3",#"53",#"6", nil];
and I want to compare these two NSMutableArray index to index (compare value to value) and tell me that which index of array in these NSMutableArrays is different. for example in two NSMutableArray on top this index is different (0,2,3,4) and I want store this values in certain Array.

NSMutableArray* differentIndex;
differentIndex = [[NSMutableArray alloc] init];
for(int i = 0 ; i < [a count]; i++) {
if(i < [b count]){
if(![b[i] isEqual:a[i]]){
[differentIndex addObject:[NSNumber numberWithInt:i]];
}
} else {
[differentIndex addObject:[NSNumber numberWithInt:i]];
}
}
Now your differentIndex contains all the different index in the arrays. :)

NSMutableArray *a = [[NSMutableArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5",#"6", nil];
NSMutableArray *b = [[NSMutableArray alloc]initWithObjects:#"11",#"2",#"5",#"3",#"53",#"6", nil];
NSMutableArray *new = [[NSMutableArray alloc]init];
for(int i =0;i<[b count]<=[a count]?[b count]:[a count];i++)
{
NSString *_keyA = [a objectAtIndex:i];
NSString *_keyB = [b objectAtIndex:i];
if ([_keyA isEqualToString:_keyB])
continue;
[new addObject:[NSString stringWithFormat:#"%d",i]];
}

Related

Twodimensional array into a UILabel

I have a twodimensional array where i i would like to print all second values of the objects into an UIlabel homePlayersFouls. the problem is i do not know how to this.
I have tried following things:
componentsJoinedByString:#"\n".
This will just print (
The other thing ive tried is this:
[[homePlayersArray objectAtIndex:i] objectAtIndex:1];
this just prints 0 since it looping and deleting the content inside label
i've checked wether there is something wrong the the array, but when i do this inside the loop:
nslog(#"%#",[[homePlayersArray objectAtIndex:i] objectAtIndex:1];);
It prints all 0 0 0 which is the 3 second values in my objects.
The question is then how can i print all my second values in all the objects in the array into an UIlabel?
here is the complete code:
for (int i=0;i<[homeNumbersArray count];i++){
NSArray *tempArray = [[NSArray alloc] initWithObjects:[homeNumbersArray objectAtIndex:i],[NSNumber numberWithInt:0], nil];
[homePlayersArray addObject:tempArray];
NSObject* someObject = [[homePlayersArray objectAtIndex:i] objectAtIndex:1];
homePlayersFouls.text = [NSString stringWithFormat:#"%#", someObject];
}
You need:
homePlayersFouls.text = [homePlayersFouls.text stringByAppendingString:[NSString stringWithFormat:#"%#", someObject]];
NSMutableString *fouls = [NSMutableString string];
for (int i=0;i<[homeNumbersArray count];i++){
NSArray *tempArray = [[NSArray alloc] initWithObjects:[homeNumbersArray objectAtIndex:i],[NSNumber numberWithInt:0], nil];
[homePlayersArray addObject:tempArray];
NSObject* someObject = [[homePlayersArray objectAtIndex:i] objectAtIndex:1];
[fouls appendFormat:#"%#", someObject];
}
homePlayersFouls.text = fouls;

sum quantity of dictionaries in array

I have a NSArray containing a list of NSDictionary. Like:
NSArray *array = ...;
NSDictionary *item = [array objectAtIndex:0];
NSLog (#"quantity: %#", [item objectForKey: #"quantity"]);
How can I sum all the quantities contained in all dictionaries of the array?
I think you can try KVC
NSMutableArray *goods = [[NSMutableArray alloc] init];
NSString *key = #"quantity";
[goods addObject:#{key:#(1)}];
[goods addObject:#{key:#(2)}];
[goods addObject:#{key:#(3)}];
NSNumber *sum = [goods valueForKeyPath:#"#sum.quantity"];
NSLog(#"sum = %#", sum);
If you call valueForKey: on an array it gives you an array of all the values for that key, so [array valueForKey:#"quantity"] will give you an array which you can loop over and sum all the values.
NSMutableArray *quantityArray = [item objectForKey: #"quantity"];
int total =0;
for(int i=0;i<[quantityArray count];i++)
{
total+= [quantityArray objectAtIndex:i];
}

Objective-C adding values of 2 arrays

I have NSArray1 = (1, 5, 2)
and NSArray2 = (1, 3, 5)
i want to array1 + array2 = (should return) = (2, 8, 7)
(in fact is it even possible to do this with NSArray)?
Heres a similar question
Adding two arrays together
(but this adds the values of the second array onto the end of the first array)
NSArray *a = [NSArray arrayWithObjects: #"1" ,#"2",#"3",nil];
NSArray *b = [NSArray arrayWithObjects: #"1" ,#"2",#"3",nil];
NSMutableArray *c = [[NSMutableArray alloc]init];
c = [a addObjectsFromArray:b];
// just a test code . . . .
If it's a C array, then just do
int newArray[3];
for (int i=0;i<3;i++)
newArray[i] = array1[i]+array2[i];
But if it's a NSArray with NSNumbers (You can't have primitives in NSArray), then just do
NSMutableArray *newArray = [NSMutableArray array];
for (int i=0;i<[array1 count];i++)
[newArray addObject:[NSNumber numberWithInt:[[array1 objectAtIndex:i] intValue]+[[array2 objectAtIndex:i] intValue]]];
//If you're using Mountain Lion, then you can use the following
//[newArray addObject:#([array1[i] intValue]+[array2[i] intValue])];
Edit:
If you have more than 1 array, then
int numArrays = 3;
NSArray *arrayOfNum = //An array of arrays that contains all the numbers
NSMutableArray *newArray = [NSMutableArray array]
for (int i=0;i<[array1 count];i++)
{
int total = 0;
for (int x=0;x<numArrays;x++)
total+=[arrayOfNum[x] intValue];
[newArray addObject:#(total)];
}

How to exclude previously randomly selected NSArrays in loop

I'm new to Objective-C and I'm trying to create a simple dictionary style app for personal use. Right now I'm attempting to make a loop that prints randomly selected NSArrays that have been added to an NSDictionary. I'd like to print each array only once. Here is the code I'm working with:
NSArray *catList = [NSArray arrayWithObjects:#"Lion", #"Snow Leopard", #"Cheetah", nil];
NSArray *dogList = [NSArray arrayWithObjects:#"Dachshund", #"Pitt Bull", #"Pug", nil];
...
NSMutableDictionary *wordDictionary = [[NSMutableDictionary alloc] init];
[wordDictionary setObject: catList forKey:#"Cats"];
[wordDictionary setObject: dogList forKey:#"Dogs"];
...
NSInteger keyCount = [[wordDictionary allKeys] count];
NSInteger randomKeyIndex = arc4random() % keyCount;
int i = keyCount;
for (i=i; i>0; i--) {
NSString *randomKey = [[wordDictionary allKeys] objectAtIndex:randomKeyIndex];
NSMutableArray *randomlySelectedArray = [wordDictionary objectForKey:randomKey];
NSLog(#"%#", randomlySelectedArray);
}
This code prints the same array "i" times. Any pointers on how to exclude previously printed arrays from being printed again?
I'm wondering if removeObjectForKey: could be of any use.
You just need to re-calculate the random key index every time you go through the loop, and then, as you suggest, use removeObjectForKey:.
Something like this:
NSArray *catList = [NSArray arrayWithObjects:#"Lion", #"Snow Leopard", #"Cheetah", nil];
NSArray *dogList = [NSArray arrayWithObjects:#"Dachshund", #"Pitt Bull", #"Pug", nil];
//...
NSMutableDictionary *wordDictionary = [[NSMutableDictionary alloc] init];
[wordDictionary setObject: catList forKey:#"Cats"];
[wordDictionary setObject: dogList forKey:#"Dogs"];
//...
while ([wordDictionary count] > 0) {
NSInteger keyCount = [wordDictionary count];
NSInteger randomKeyIndex = arc4random() % keyCount;
NSString *randomKey = [[wordDictionary allKeys] objectAtIndex:randomKeyIndex];
NSMutableArray *randomlySelectedArray = [wordDictionary objectForKey:randomKey];
NSLog(#"%#", randomlySelectedArray);
[wordDictionary removeObjectForKey: randomKey];
}
In your code, you generate a random randomKeyIndex, then use it without changing its value i times in the loop. So you get i times the same array.
NSInteger randomKeyIndex = arc4random() % keyCount;
// ...
for (i=i; i>0; i--) {
NSString *randomKey = [[wordDictionary allKeys] objectAtIndex:randomKeyIndex];
// ...
}
As you say removeObjectForKey is an option for you, you can change your code into something like this:
NSInteger keyCount = [[wordDictionary allKeys] count];
for (i=keyCount; i>0; i--) {
NSInteger randomKeyIndex = arc4random() % keyCount;
NSString *randomKey = [[wordDictionary allKeys] objectAtIndex:randomKeyIndex];
NSMutableArray *randomlySelectedArray = [wordDictionary objectForKey:randomKey];
[wordDictionary removeObjectForKey:randomKey];
keyCount--;
NSLog(#"%#", randomlySelectedArray);
}

finding a number in array

I have an Array {-1,0,1,2,3,4...}
I am trying to find whether an element exist in these number or not, code is not working
NSInteger ind = [favArray indexOfObject:[NSNumber numberWithInt:3]];
in ind i am always getting 2147483647
I am filling my array like this
//Loading favArray from favs.plist
NSString* favPlistPath = [[NSBundle mainBundle] pathForResource:#"favs" ofType:#"plist"];
NSMutableDictionary* favPlistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:favPlistPath];
NSString *favString = [favPlistDict objectForKey:#"list"];
NSArray *favList = [favString componentsSeparatedByString:#","];
//int n = [[favList objectAtIndex:0] intValue];
favArray = [[NSMutableArray alloc] initWithCapacity:100];
if([favList count]>1)
{
for(int i=1; i<[favList count]; i++)
{
NSNumber *f = [favList objectAtIndex:i];
[favArray insertObject:f atIndex:(i-1)];
}
}
That's the value of NSNotFound, which means that favArray contains no object that isEqual: to [NSNumber numberWithInt:3]. Check your array.
After second edit:
Your favList array is filled with NSString objects. You should convert the string objects to NSNumber objects before inserting them in favArray:
NSNumber *f = [NSNumber numberWithInt:[[favList objectAtIndex:i] intValue]];