Nick Red
Nick Red

Reputation: 396

get average index of objects that are in an array of arrays

I have an array of an array of objects. The inner arrays have been sorted in order then added to an overall array. All the inner objects are the same thing with different values.

I am trying to go through those arrays and organize the objects in order from the average index value.

Example of the inner arrays sorted

obj 1  | obj 2 | obj 2
obj 2  | obj 1 | obj 1
obj 3  | obj 3 | obj 4
obj 4  | obj 4 | obj 3

then the output i would need from that after getting the average would be

obj 2
obj 1
obj 3
obj 4

I really only need the top three index averages but I would like to get all of them. So for example to get 3 I could do this

for (NSArray* innerArray in outterArray) {
            for (NSString* str in innerArray) {

                if ([innerArray indexOfObject:str] == 0) {

                    [first addObject:str];
                }else if([innerArray indexOfObject:str] == 1){

                    [second addObject:str];

                }else if ([innerArray indexOfObject:str] == 2){
                    [third addObject:str];

                }


            }
        }

Then go through those three arrays and see what pops up where but there must be a better way of doing this and its probably something simple but I cant see it

Upvotes: 0

Views: 164

Answers (1)

Martin R
Martin R

Reputation: 539965

All objects occur the same number of times, therefore you can compute the sum of indices instead of the average for each object.

This can be done by enumerating once over all inner dictionaries, and updating a hash map (dictionary) with the sum of indices for the current object. (Note that indexOfObject: is not needed here to locate the objects in the inner array.)

Then sort the objects according to the sum of the indices (which is the value of the object in the dictionary):

NSArray *outerArray = @[
                        @[@"obj 1", @"obj 2", @"obj 3", @"obj 4"],
                        @[@"obj 2", @"obj 1", @"obj 3", @"obj 4"],
                        @[@"obj 2", @"obj 1", @"obj 4", @"obj 3"],
                        ];

NSMutableDictionary *map = [NSMutableDictionary dictionary];
for (NSArray *innerArray in outerArray) {
    NSUInteger index = 0; // Index of next object in the inner array
    for (NSString *str in innerArray) {
        // Add current index of this object to previous sum and update hash map
        NSUInteger tmp = index + [map[str] unsignedIntegerValue];
        map[str] = @(tmp);
        index++;
    }
}

NSArray *sorted = [[map allKeys] sortedArrayUsingComparator:^NSComparisonResult(NSString *obj1, NSString *obj2) {
    return [map[obj1] compare:map[obj2]];
}];

NSLog(@"%@", sorted);

Output:

(
    "obj 2",
    "obj 1",
    "obj 3",
    "obj 4"
)

The dictionary map in this case is

{
    "obj 1" = 2;     // "obj 1" at indices 0 + 1 + 1
    "obj 2" = 1;     // "obj 2" at indices 1 + 0 + 0
    "obj 3" = 7;     // "obj 3" at indices 2 + 2 + 3
    "obj 4" = 8;     // "obj 4" at indices 3 + 3 + 2
}

Upvotes: 1

Related Questions