Mohamed Nazir
Mohamed Nazir

Reputation: 105

Sum objectAtIndex values in NSArray of NSArray OR Add value with existing value for key in NSDictionary

Any One Solution I need. Suggest me best with answer.

I have hundreds of array like that give me Best Solution. I didn't like Nested ForLoops

1. I need Distinct Union the Array By ObjectAtIndex : 0 , and also sum their value in ObjectAtIndex : 1.

A--> [[1,"100.0"],[2,"100.0"],[2,"100.0"],[3,"100.0"],[4,"100.0"],[3,"100.0"],[4,"100.0"]]

B--> [[1,250],[2,250],[1,250],[3,"200.2"],[1,"200.2"],[4,"200.2"],[1,"200.2"],[4,"200.2"]]

I need like that A--> [[1,"100.0"],[2,"200.0"],[3,"200.0"],[4,"200.0"]]

2.I have to setObject A and B array in Dictionary. But if the key exist in dictionary sum that value with exiting value.

{

 A =     {
    1 = 100;
    2 = 100;
    3 = 100;
    4 = 100;
};

 B =     {
    1 = 250;
    2 = 250;
    3 = "200.2";
    4 = "200.2";
};

}

Like

NSArray* uniqueValues = [data valueForKeyPath:[NSString stringWithFormat:@"@distinctUnionOfObjects.%@", @"self"]];

Upvotes: 0

Views: 243

Answers (2)

Vignesh Kumar
Vignesh Kumar

Reputation: 598

Alternative solution based on KVC Collection Operators.

    NSArray* arrayOFArray = @[@[@1, @100.0],@[@2, @100.0],@[@3, @100.0],@[@1, @100.0],@[@1, @100.0]];

    NSMutableDictionary* sumDic = [NSMutableDictionary new];
    NSInteger index = 0; // By changing index position. You get unique sum either way.
    for (NSArray* item in arrayOFArray)
    {
       id key = item[index];
        if (key && sumDic[key] == nil) {

            NSArray *keyArray = [self findSameValueKey:arrayOFArray WithKey:key withIndex:index];
            NSArray *unionArray = [keyArray valueForKeyPath: @"@unionOfArrays.self"];
            NSNumber *sum = [unionArray valueForKeyPath:@"@sum.floatValue"];
            sumDic[key] = [NSNumber numberWithInt:[sum floatValue] - ([keyArray count]* [key floatValue])];
        }
    }

    NSLog(@"%@", sumDic.description);


    // Helps to find same value key.
    -(NSArray*)findSameValueKey:(NSArray*)dateArray WithKey:(NSString*)key withIndex:(NSInteger)index {
        NSPredicate *predicate = [NSPredicate predicateWithBlock:^BOOL(NSArray *resultsArray, NSDictionary *bind){
            if ([resultsArray[index] isEqual:key]) {
                return true;
            }
            return false;
        }];

        // Apply the predicate block .
        NSArray *sameValueKeys = [dateArray filteredArrayUsingPredicate:predicate];
        return sameValueKeys;
    }

Upvotes: 0

oxigen
oxigen

Reputation: 6263

NSArray* A = @[@[@1, @100.0],@[@2, @100.0],@[@3, @100.0],@[@1, @100.0],@[@1, @100.0]];

NSMutableDictionary* sum = [NSMutableDictionary new];
for (NSArray* item in A)
{
    id key = item[0];
    if (sum[key] == nil)
    {
        sum[key] = item[1];
    }
    else
    {
        sum[key] = @([sum[key] floatValue] + [item[1] floatValue]);
    }
}

NSLog(@"%@", sum.description);

Output:

3 = 100;
1 = 300;
2 = 100;

Upvotes: 1

Related Questions