1

I have 10 NSMutableArrays and I need to compare them by its count and select the name of the array which has the lowest count - [array count]. What would be the best and fastest method to do so?

I tried to add the names of all arrays into another array, loop through with 'for' method, but it doesn't work as NSUInteger conflicts with ID

There must be simpler method, I am sure of it. I just can't find the right reference on google...

Can you please point me the right direction?

Many thanks a.

2

2 Answers 2

0

first of all create array of counts of all the arrays.

than apply quicksort algorithm

here is c program of algorithm c program of quicksort

you just have to pass the newly created array in first parameter, second parameter is 0(zero) and third paramter is newly created array count - 1 in quickshort method.

get the sorted array than get first index is lowest count and last index is the highest count get index and find which array has highest and lowest count.

if you don't want to create array of count than you can directly create array of arrays but it occupies the memory so best way is to create array of count.

Sign up to request clarification or add additional context in comments.

Comments

0

I found solution. Two actually. After getting all counts from all 10 arrays into new array "integers", finding smallest and highest value is easy with:

NSNumber *max=[integers valueForKeyPath:@"@max.self"];
NSNumber *min=[integers valueForKeyPath:@"@min.self"];

NSLog(@"max: %@ min: %@", max, min);

// get indexOfObject value
int indexOfMin = (int)[integers indexOfObject:min];
NSLog(@"Index: %i", indexOfMin);

Second method is:

float xmax = -MAXFLOAT;
float xmin = MAXFLOAT;
for (NSNumber *num in integers) {
    float x = [num floatValue];
    if (x < xmin) xmin = x;
    if (x > xmax) xmax = x;

    NSLog(@"integer: %f",x );
}

Thank you for pointing me the right direction!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.