So I have three different arrays involved here...
I'm looping through golferThreeIcons (NSMutableArray), and I'm trying to take the highest 4 results (golferThreeIcons contains #'s as strings) and store them into the topFourIconsNum (NSMutableArray) and then I plan to store the id of that highest number in the array topFourIconsId (NSMutableArray).
One tricky thing is that golferThreeIcons start out at 99, but I want that to act like zero. So the number 99 should not get added to either of the arrays...
Example:
golferThreeIcons {2,1,5,3,9}
And after the loop went through I want it to show something like this...
topFourIconsId {0,2,3,4} --- 0 corresponds to 2 in golferThreeIcons --- 2 corresponds to 5 in golfer three icons
topFourIconsNum {2,5,3,9} ----(in any order as long as it corresponds to the top four icons id)
NSMutableArray *topFourIconsId = [NSMutableArray arrayWithObjects: #"0", #"0", #"0", #"0", #"0" ,nil];
NSMutableArray *topFourIconsNum = [NSMutableArray arrayWithObjects: #"0", #"0", #"0", #"0", #"0" ,nil];
int *ids = 0;
for (NSString *s in golferThreeIconCounter) {
if(s != #"0") {
int sint = [s intValue];
int *idn = 0;
for(NSString *n in topFourIconsNum) {
int nint= [n intValue];
if(sint == 99 && nint == 0) {
NSString *idstring = [NSString stringWithFormat:#"%d", ids];
NSString *sintstring = [NSString stringWithFormat:#"%d", sint];
[topFourIconsId replaceObjectAtIndex:idn withObject:idstring];
[topFourIconsNum replaceObjectAtIndex:idn withObject:sintstring];
NSLog(#"IN %#",sintstring);
break;
}
else {
if (sint > nint) {
NSString *idstring = [NSString stringWithFormat:#"%d", ids];
NSString *sintstring = [NSString stringWithFormat:#"%d", sint];
[topFourIconsId replaceObjectAtIndex:idn withObject:idstring];
[topFourIconsNum replaceObjectAtIndex:idn withObject:sintstring];
NSLog(#"IN %#",sintstring);
break;
}
}
idn++;
}
}
ids++;
}
Just a crazy idea
Put your golferThreeIcons in a NSDictionary with the array index as the key and the array value as the value and then sort the keys on their value:
NSDictionary *dict = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:#"2", #"1", #"5", #"3", #"9", nil] forKeys:[NSArray arrayWithObjects:#"0", #"1", #"2", #"3", #"4", nil]];
NSArray *sortedKeys = [dict keysSortedByValueUsingSelector:#selector(caseInsensitiveCompare:)];
NSLog([sortedKeys description]);
Related
I have an array with multiple locations for different states.
{"location":"Lekki Phase 1","state":"abc","country":"Nigeria"},
{"location":"Lekki Phase 2","state":"xyz","country":"Nigeria"},
{"location":"Osapa London1","state":"def","country":"Nigeria"},
{"location":"Lekki Phase 2","state":"abc","country":"Nigeria"},
{"location":"Lekki Phase 3","state":"xyz","country":"Nigeria"},
{"location":"Osapa London 2","state":"def","country":"Nigeria"},..........
Now i can make an array for different states with no duplicate state , like
{"abc","xyz","def"}
But what i want is to display all locations state wise in a table.
How can i do this??
Using NSPredicate we can efficiently filter this . I have tried and tested working for me.
Here 'allDataArray' is array with dictionaries, You can replace your array here (the first one in your post)
NSMutableArray *allDataArray = [[NSMutableArray alloc] init];
NSMutableDictionary *dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:#"Lekki Phase 1" forKey:#"location"];
[dict1 setObject:#"abc" forKey:#"state"];
[dict1 setObject:#"Nigeria" forKey:#"country"];
[allDataArray addObject:dict1];
dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:#"Lekki Phase 2" forKey:#"location"];
[dict1 setObject:#"xyz" forKey:#"state"];
[dict1 setObject:#"Nigeria" forKey:#"country"];
[allDataArray addObject:dict1];
dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:#"Lekki Phase 2" forKey:#"location"];
[dict1 setObject:#"abc" forKey:#"state"];
[dict1 setObject:#"Nigeria" forKey:#"country"];
[allDataArray addObject:dict1];
dict1 = [[NSMutableDictionary alloc] init];
[dict1 setObject:#"Lekki Phase 3" forKey:#"location"];
[dict1 setObject:#"xyz" forKey:#"state"];
[dict1 setObject:#"Nigeria" forKey:#"country"];
[allDataArray addObject:dict1];
//NSLog(#"%#",allDataArray);
NSArray *state = [NSArray arrayWithObjects:#"abc",#"xyz", nil];
NSMutableArray *locationInState = [[NSMutableArray alloc] initWithCapacity:[state count]];
for(int i=0; i< [state count]; i++)
{
NSMutableArray *filteredarray = [[allDataArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:#"(state == %#)", [state objectAtIndex:i]]] mutableCopy];
for(int j=0; j<[filteredarray count];j++)
{
NSDictionary *dict = [filteredarray objectAtIndex:j];
[filteredarray replaceObjectAtIndex:j withObject:[dict valueForKey:#"location"]];
}
[locationInState addObject:filteredarray];
}
NSLog(#"%#",locationInState);
Here locationInState array contains all location for filetred state. You can map them easily by index.
Result is
(
(
"Lekki Phase 1",
"Lekki Phase 2"
),
(
"Lekki Phase 2",
"Lekki Phase 3"
)
)
First It not array but it is Dictionary.
NSMutableDictionary * newDict = [NSMutableDictionary dictionaryWithCapacity:[OLdDict count]];
for(id item in [OLdDict allValues]){
NSArray * keys = [OLdDict allKeysForObject:item];
[newDict setObject:item forKey:[[OLdDict allKeysForObject:item] objectAtIndex:0]];
}
NSMutableDictionaries can act as uniquing collections (because they replace objects for keys if the same key is used twice). We can also take advantage of the fact that NSString is generally a constant address location, and funnel each one of those dictionaries into an array. To unique out each array of dictionaries, it would be far easier to wrap them in an object, but here goes:
-(void)uniquingSort {
//Setup collections for the uniquing process
NSMutableArray *datasource = //...
NSMutableIndexSet *hits = [NSMutableIndexSet indexSet];
NSMutableDictionary *uniquingDict = #{}.mutableCopy;
//Setup an index for the indexed set
int idx = 0;
//iterate through the array of dictionaries
for (NSArray *arrOfDicts in datasource) {
//get the dictionary we want to unique against
NSDictionary *innerDict = arrayOfDicts[1];
//do we have a dupe? If so, add its index to the index set
if (uniquingDict[innerDict[#"state"]] != nil)
[hits addIndex:idx];
uniquingDict[innerDict[#"state"]] = innerDict[#"state"];
idx++;
}
//cut out all the hits till we are only uniqued for the "state" key
[datasource removeObjectsAtIndexes:hits];
}
I have input as two arrays shown below
NSArray *array1=[[NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
NSArray *array2=[[NSArray alloc]initWithObjects:#"1",#"2",#"1", nil];
the output should resemble like this.
the same element should be cancelled only one time.
NSArray *array3=[[NSArray alloc]initWithObjects:#"1",#"2", nil];
THANKS IN ADVANCE.....
NSArray *array1 = #[#"1",#"2",#"3"];
NSArray *array2 = #[#"1",#"2",#"1"];
NSMutableSet *allElemets = [NSSet setWithArray:array1];
[allElemets addObjectsFromArray:array2];
This will return you all elements without duplicates.
In this case it will be
#"1",#"2",#"3"
Edit:
This will return the intersection of the arrays
NSMutableSet *set1 = [NSMutableSet setWithArray:array1];
NSSet *set2 = [NSSet setWithArray:array2];
[set1 intersectSet:set2];
Use NSCountedSet for the above situation
NSMutableArray *array1=[[NSMutableArray alloc]initWithObjects:#"r",#"a",#"r",#"r",#"r", nil];
NSArray *array2=[[NSArray alloc]initWithObjects:#"b",#"c",#"r", nil];
NSMutableSet *setOne = [NSMutableSet setWithArray: array1];
NSSet *setTwo = [NSSet setWithArray: array2];
[setOne unionSet:setTwo];
NSArray *arrayOneResult = [setOne allObjects];
NSLog(#"%#",arrayOneResult);
NSMutableArray *resultArray = [[NSMutableArray alloc]init];
NSCountedSet *set = [[NSCountedSet alloc] initWithArray:arrayOneResult];
for (id item in set)
{
NSCountedSet *set1 = [[NSCountedSet alloc] initWithArray:array1];
NSCountedSet *set2 = [[NSCountedSet alloc]initWithArray:array2];
int diff = abs([set1 countForObject:item] - [set2 countForObject:item]);
for (int i = 0 ;i < diff ;i++ ) {
[resultArray addObject:item];
}
}
NSLog(#"the array : %#",resultArray );
f you are fine with sets instead of arrays, you can use NSMutableSet instead of NSArray. NSMutableSet has nice methods like intersectSet: and minusSet:
if([[array1 objectAtIndex:i] isEqualToString:[array2 objectAtIndex:i]])
{
[array2 removeObjectAtIndex: i];
NSLog(#"same element removed.");
}
array3 = [firstArray arrayByAddingObjectsFromArray:secondArray];
or
NSMutableSet *set = [NSMutableSet setWithArray:array1];
[set addObjectsFromArray:array2];
array3 = [set allObjects];
Two arrays are compared and duplicate values are removed, you get your values.
Here tHe Code goes
EDIt: This WOuld remove the Duplicate Value add Unique value.
NSArray *array1=[[NSArray alloc]initWithObjects:#"1",#"2",#"3", nil];
NSArray *array2=[[NSArray alloc]initWithObjects:#"1",#"2",#"1", nil];
//Here Create nEw Array with Arra1
NSMutableArray * newArray =[[NSMutableArray alloc] initWithArray:array1];
for(int index=0; index<[array2 count];index++)
{
id object =[array2 objectAtIndex:index];
if(![newArray containsObject:object])//this methods Returns YES/NO
{
[newArray addObject: object];
}
}
I have made a NSDictionary HospitalDictionary which holds latitude,longitude and distances
NSMutableArray *HospitalArray = [NSMutableArray array];
NSDictionary *HospitalDictionary = [[NSDictionary alloc]init];
NSString *LATITUDE = #"latitude";
NSString *LONGITUDE = #"longitude";
NSString *DISTANCE = #"distance";
for (int i=0; i<=100; i++) {
// calculations for coordinates and distance are done ....
HospitalDictionary = [NSDictionary dictionaryWithObjectsAndKeys:latitude, LATITUDE,
longitude, LONGITUDE,[NSNumber numberWithInt:distanceInKm], DISTANCE, nil];
[HospitalArray addObject:HospitalDictionary];
{
Then data is short using following code
// These line are added to short the Dictionary added to Array List with the Key of Distance
NSSortDescriptor *distanceDescriptor = [[NSSortDescriptor alloc] initWithKey:DISTANCE ascending:YES];
id obj;
NSEnumerator * enumerator = [HospitalArray objectEnumerator];
NSArray *descriptors = [NSArray arrayWithObjects:distanceDescriptor, nil];
NSArray *sortedArray = [HospitalArray sortedArrayUsingDescriptors:descriptors];
enumerator = [sortedArray objectEnumerator];
// this will print out the shorted list for DISTANCE
while ((obj = [enumerator nextObject])) NSLog(#"%#", obj);
// this will return the object at index 1
NSLog(#"Selected array is %#",[sortedArray objectAtIndex:1]);
Output of this is -
2012-05-30 08:24:42.784 Hospitals[422:15803]
sorted array is
{
distance = 1;
latitude = "27.736221";
longitude = "85.330095";
}
I want to get only Latitude or Longitude for the ObjectAtIndex:1 for the sortedArray. How can i get the particular value ie. latitude out of sortedArray not the complete list.
To access a property/method of an object in an array, you can use something like this:
NSLog(#"Latitude for object at index 1 is %#",[[sortedArray objectAtIndex:1] latitude]);
I need to perform what I feel is a basic function but I can't find any documentation on how to do it. Please help!
I need to count how many times a certain object occurs in an array. See example:
array = NSArray arrayWithObjects:#"Apple", #"Banana", #"Cantaloupe", #"Apple", #"DragonFruit", #"Eggplant", #"Apple", #"Apple", #"Guava",nil]retain];
How can I iterate through the array and count the number of times it finds the string #"Apple"?
Any help is appreciated!
One more solution, using blocks (working example):
NSInteger occurrences = [[array indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {return [obj isEqual:#"Apple"];}] count];
NSLog(#"%d",occurrences);
As #bbum said, use an NSCounted set. There is an initializer thet will convert an array directly into a counted set:
NSArray *array = [[NSArray alloc] initWithObjects:#"A", #"B", #"X", #"B", #"C", #"D", #"B", #"E", #"M", #"X", nil];
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];
NSLog(#"%#", countedSet);
NSLog output:
(D [1], M [1], E [1], A [1], B [3], X [2], C [1])
Just access items:
count = [countedSet countForObject: anObj]; ...
A Simple and specific answer:
int occurrences = 0;
for(NSString *string in array){
occurrences += ([string isEqualToString:#"Apple"]?1:0); //certain object is #"Apple"
}
NSLog(#"number of occurences %d", occurrences);
PS: Martin Babacaev's answer is quite good too. Iteration is faster with blocks but in this specific case with so few elements I guess there is no apparent gain. I would use that though :)
Use an NSCountedSet; it'll be faster than a dictionary and is designed to solve exactly that problem.
NSCountedSet *cs = [NSCountedSet new];
for(id anObj in someArray)
[cs addObject: anObj];
// then, you can access counts like this:
.... count = [cs countForObject: anObj]; ...
[cs release];
Just came across this pretty old question. I'd recommend using a NSCountedSet:
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];
NSLog(#"Occurrences of Apple: %u", [countedSet countForObject:#"Apple"]);
I would encourage you to put them into a Dictionary (Objective C's version of a map). The key to the dictionary is the object and the value should be the count. It should be a MutableDictionary of course. If the item is not found, add it and set the count to 1.
- (int) numberOfOccurrencesForString:(NSString*)needle inArray:(NSArray*)haystack {
int count = 0;
for(NSString *str in haystack) {
if([str isEqualToString:needle]) {
count++;
}
}
return count;
}
I up-voted Rob's answer, but I wanted to add some code that I hope will be of some assistance.
NSArray *array = [[NSArray alloc] initWithObjects:#"A", #"B", #"B", #"B", #"C", #"D", #"E", #"M", #"X", #"X", nil];
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
for(int i=0; i < [array count]; i++) {
NSString *s = [array objectAtIndex:i];
if (![dictionary objectForKey:s]) {
[dictionary setObject:[NSNumber numberWithInt:1] forKey:s];
} else {
[dictionary setObject:[NSNumber numberWithInt:[dictionary objectForKey:s] intValue]+1 forKey:s];
}
}
for(NSString *k in [dictionary keyEnumerator]) {
NSNumber *number = [dictionary objectForKey:k];
NSLog(#"Value of %#:%d", k, [number intValue]);
}
If the array is sorted as in the problem statement then you don't need to use a dictionary.
You can find the number of unique elements more efficiently by just doing 1 linear sweep and incrementing a counter when you see 2 consecutive elements being the same.
The dictionary solution is O(nlog(n)), while the linear solution is O(n).
Here's some pseudo-code for the linear solution:
array = A,B,B,B,B,C,C,D,E,M,X,X #original array
array = array + -1 # array with a dummy sentinel value to avoid testing corner cases.
# Start with the first element. You want to add some error checking here if array is empty.
last = array[0]
count = 1 # you have seen 1 element 'last' so far in the array.
for e in array[1..]: # go through all the elements starting from the 2nd one onwards
if e != last: # if you see a new element then reset the count
print "There are " + count + " " + last elements
count = 1 # unique element count
else:
count += 1
last = e
the complete code with reference to #bbum and #Zaph
NSArray *myArray = [[NSArray alloc] initWithObjects:#"A", #"B", #"X", #"B", #"C", #"D", #"B", #"E", #"M", #"X", nil];
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:myArray];
for (NSString *item in countedSet) {
int count = [countedSet countForObject: item];
NSLog(#"the String ' %# ' appears %d times in the array",item,count);
}
Thank you.
If you want it more generic, or you want to count equals/different objects in array, try this:
Sign "!" count DIFFERENT values. If you want SAME values, remove "!"
int count = 0;
NSString *wordToCheck = [NSString string];
for (NSString *str in myArray) {
if( ![str isEqualToString:wordToCheck] ) {
wordToCheck = str;
count++;
}
}
hope this helps the community!
I've used it to add correct number of sections in uitableview!
You can do this way,
NSArray *array = [[NSArray alloc] initWithObjects:#"A", #"B", #"X", #"B", #"C", #"D", #"B", #"E", #"M", #"X", nil];
NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:array];
NSArray *uniqueStates = [[orderedSet set] allObjects];
NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:array];
for(int i=0;i<[uniqueStates count];i++){
NSLog(#"%# %d",[uniqueStates objectAtIndex:i], [countedSet countForObject: [uniqueStates objectAtIndex:i]]);
}
The result is like : A 1
Being new to Cocoa, and probably not knowing all of the potential classes available that already have this functionality neatly wrapped in an OO class, here's an algorithm inquiry. What's the best bet to count how many times a particular key occurs in an array of multiple NSDictionary instances?
Essentially my data structure (in this case an NSArray) might contain several NSDictionary instances at any given time, each one having the same keys, potentially different values. Some values repeat. I'd like to be able to know how many times a particular key/value appears. Example:
{
foo => 1,
bar => 2
}
{
foo => 1,
bar => 3
}
{
foo => 2,
bar => 1
}
In this case I'm interested that foo=>1 occured 2 times and foo=>2 occured 1 time. Is building an instance of NSCountedSet the best way to go about this? Perhaps a C linked-list?
You may want to rethink how you are structuring your data. I'd track something like this while adding to the NSArray instead of trying to discover it at a later time. You might create a new class to handle adding and removing the data so that you can keep your own counts of the data.
NSDictionary * dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:1], #"foo",
[NSNumber numberWithInt:2], #"bar", nil];
NSDictionary * dict2 = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:1], #"foo",
[NSNumber numberWithInt:3], #"bar", nil];
NSDictionary * dict3 = [[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithInt:2], #"foo",
[NSNumber numberWithInt:1], #"bar", nil];
NSArray * arrayOfDictionaries = [[NSArray alloc] initWithObjects:
dict1, dict2, dict3, nil];
// count all keys in an array of dictionaries (arrayOfDictionaries):
NSMutableDictionary * countKeys = [[NSMutableDictionary alloc] initWithCapacity:0];
NSCountedSet * counts = [[NSCountedSet alloc] initWithCapacity:0];
NSArray * keys;
NSString * pairString;
NSString * countKey;
for (NSDictionary * dictionary in arrayOfDictionaries)
{
keys = [dictionary allKeys];
for (NSString * key in keys)
{
pairString = [NSString stringWithFormat:#"%#->%#", key, [dictionary valueForKey:key]];
if ([countKeys valueForKey:pairString] == nil)
{
[countKeys setValue:[NSString stringWithString:pairString] forKey:pairString];
}
countKey = [countKeys valueForKey:pairString];
{ [counts addObject:countKey]; }
}
}
NSLog(#"%#", counts);
[counts release];
[countKeys release];
[arrayOfDictionaries release];
[dict1 release];
[dict2 release];
[dict3 release];
NSCountedSet *keyCounts = [NSCountedSet set];
for (NSDictionary *dict in myDictionaries)
[keyCounts unionSet:[NSSet setWithArray:[dict allKeys]]];