0

I have an array which contains this objects:

(
    {
        aOptns =             (
        );
        fCustomDscnt = 0;
        fPrcntDscnt = 0;
        fPrice = 0;
        fQty = 1;
        iItemDayPriceId = 143;
        iShiftId = 1;
        sItemName = "";
        sModifier = "";
    },
    {
        aOptns =             (
        );
        fCustomDscnt = 0;
        fPrcntDscnt = 0;
        fPrice = 0;
        fQty = 1;
        iItemDayPriceId = 143;
        iShiftId = 1;
        sItemName = "";
        sModifier = "";
    },
    {
        aOptns =             (
        );
        fCustomDscnt = 0;
        fPrcntDscnt = 0;
        fPrice = 0;
        fQty = 1;
        iItemDayPriceId = 143;
        iShiftId = 1;
        sItemName = "";
        sModifier = "";
    },
    {
        aOptns =             (
        );
        fCustomDscnt = 0;
        fPrcntDscnt = 0;
        fPrice = 0;
        fQty = 1;
        iItemDayPriceId = 112;
        iShiftId = 1;
        sItemName = "";
        sModifier = "";
    }
)

I need to merge the contents of array if the objects are same and modify the object inside that array in such a way that it should be like this:

(
        {
            aOptns =             (
            );
            fCustomDscnt = 0;
            fPrcntDscnt = 0;
            fPrice = 0;
            fQty = 3;
            iItemDayPriceId = 143;
            iShiftId = 1;
            sItemName = "";
            sModifier = "";
        },

        {
            aOptns =             (
            );
            fCustomDscnt = 0;
            fPrcntDscnt = 0;
            fPrice = 0;
            fQty = 1;
            iItemDayPriceId = 112;
            iShiftId = 1;
            sItemName = "";
            sModifier = "";
        }
    )

As you can see, the entry for object with iItemDayPriceId = 143 becomes 1 only with fQty = 3.

I have tried using the code here: How to Find Duplicate Values in Arrays?

But it is only comparing 2 objects at a time.

2 Answers 2

0

Edit: Oops, I missed your count requirement.

Edit 2: And the fact that you wanted to mutate the given array rather the build a new one.

Depends on what you mean by distinct but since it looks like you mean to compare the contents and not the references, you should build a new array so that if the comparison is true you increment the count and otherwise you add the object.

Mutate existing array

Disclaimer: probably not my best work here.

I would appreciate if someone could suggest how to improve this because I am pretty sure it is not the best way to do this and may not even work (don't have access to an Obj-C compiler at the moment).

Foo objectA = [Foo new];
objectA.Bar = @"Bar";
Foo objectB = [Foo new];
objectB.Bar = @"Bar";
Foo objectC = [Foo new];
objectC.Bar = @"Not Bar";

NSMutableArray *array = @[objectA, objectB, objectC];

NSMutableDictionary *countDictionary = [[NSMutableDictionary alloc] init];

for (Foo *foo in [array copy])
{
    bool found = false;

    for (Foo *key in countDictionary)
    {
        if ([key isEqualTo:foo])
        {
            NSNumber *currentCount = [countDictionary objectForKey:key];
            int currentIntCount = [currentCount intValue];
            currentIntCount++;
            [countDictionary setObject:[NSNumber numberWithInt:currentIntCount] forKey:key];

            [array removeObject:key];

            found = true;

            break;
        }

        if (!found)
            [countDictionary setObject:[NSNumber numberWithInt:1] forKey:foo];
    }
}

for (Foo *realFoo in array)
{
    NSNumber *countNumber = [countDictionary objectForKey:realFoo];
    int countInt = [countNumber intValue];

    realFoo.Count = countInt;
}

Build new array

Foo objectA = [Foo new];
objectA.Bar = @"Bar";
Foo objectB = [Foo new];
objectB.Bar = @"Bar";

NSMutableArray *array = @[objectA, objectB];
NSMutableArray *distinct = [[NSMutableArray alloc] init];

for (Foo *f in array)
{
    bool found = false;
    for (Foo *d in distinct)
    {
        if ([d isEqualTo:f])
        {               
            found = true;
            d.Count++;
        }
        break;
    }

    if (!found)
        [distinct addObject:f];
}

And of course you would have to define an equality comparer for Foos, in this case for instance

-(bool) isEqualTo:(Foo *)nFoo
{
    bool isEqual = false;

    if ([self.Bar isEqualToString:nFoo.Bar])
         isEqual = true;

    return isEqual;
}

Having not dealt with Objective C in a while I should say that there may be some best-practices as to implementing equality comparison instead of rolling your own completely like this example

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

1 Comment

No problem, but actually not quite - your requirement was to mutate the given array rather than build a new one. I'm revising my answer to do that even though I'm not sure it is desirable
0

Use below code:

 NSMutableArray* myArray =
 [[NSMutableArray alloc]initWithObjects:
 @"red",@"blue",@"red",@"green",@"yellow", @"33", @"33",@"red", @"123", @"123",nil];

 NSOrderedSet *mySet = [[NSOrderedSet alloc] initWithArray:myArray];
 myArray = [[NSMutableArray alloc] initWithArray:[mySet array]];

 NSLog(@"%@",myArray);

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.