1

I have an array (bigArray from the example) that contains multiply arrays containing strings. I need to add to bigArray another array (such as arrayA) but I want to check if an array like that already exists. If it exists I don't want to add it. The order of the small arrays (such as arrayX from the example) doesn't differentiate them from one another so if I already have an array such as arrayA containing the same arrays but in different order,(arrayZ,arrayY, arrayX instead of arrayX,arrayY,arrayZ but with the same string content) that array won't be added to the big array.

How can I accomplish this?

Examples:

Arrays example:

-bigArray

--arrayA

----arrayX -> 16,4,5,6,64

----arrayY -> 1,3,6,72,14

----arrayZ -> 13,73,50,34

--arrayB

----arrayX -> 1,4,5,6,4,2

----arrayY -> 1,4,6,12,14

----arrayZ -> 13,33,50,34

The order of the small arrays doesn't differentiate them:

--arrayc

----array -> 16,4,5,6,64

----array -> 1,3,6,72,14

is the same as:

--arrayd

----array -> 1,3,6,72,14

----array -> 16,4,5,6,64

Therefore arrayD won't be added to the big array.

4 Answers 4

1

If you use NSSet instead of NSArray you get the uniquing for free. The result is a (mutable) set of (immutable) sets of arrays.

    NSSet *setA = [NSSet setWithArray:@[   // arrayA
        @[@16,@4,@5,@6,@64],               // arrayX
        @[@1,@3,@6,@72,@14],               // arrayY
        @[@13,@73,@50,@34],                // arrayZ
    ]];

    NSSet *setB = [NSSet setWithArray:@[   // arrayB
        @[@1,@4,@5,@6,@4,@2],              // arrayX
        @[@1,@4,@6,@12,@14],               // arrayY
        @[@13,@33,@50,@34],                // arrayZ
    ]];

    NSMutableArray *bigSet = [NSMutableSet setWithArray:@[setA, setB]];

    NSLog(@"%lu", [bigSet count]);

This prints "2", as expected.

    NSSet *setC = [NSSet setWithArray:@[   // arrayC
        @[@1,@4,@6,@12,@14],               // arrayY
        @[@1,@4,@5,@6,@4,@2],              // arrayX
        @[@13,@33,@50,@34],                // arrayZ
    ]];

    [bigSet addObject:setC];

    NSLog(@"%lu", [bigSet count]);

Still prints "2", because setC and setB are equal.

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

2 Comments

That's really interesting. what will happen if I'll try to add NSSet *setB = [NSSet setWithArray:@[ // arrayC @[@4,@1,@5,@6,@4,@2], // arrayX @[@1,@4,@6,@12,@14], // arrayY @[@13,@33,@50,@34], // arrayZ ]]; to bigSet? (I've taken arrayB and switched between the first two numbers of arrayX)
@Sha In your question you specified that the inner collections (or strings?) preserve and compare order of their elements. As a result if you change the order setB would not compare equal to another set in bigSet. bigSet would contain another element as a result. If you want to change this behavior, just change the inner arrays (x, y, z) to sets as well.
1

You could try iterating through the objects of the array you want to add (ArrayB) in your example, and try - (BOOL)containsObject:(id)anObject for the checking whether the smaller arrays (x, y, and ,z) are present in the Array A.

if(ArrayA.count!=ArrayB.count){
    //Don't check because the arrays will not be same, so add ArrayB
}else{
    int i=0,counter=0;
    for(i=0;i<ArrayB.count;i++){
        if(![ArrayA contains [ArrayB objectAtIndex:i]]){
            counter = 1;
        }
    }

    if(counter==1){
       //Add ArrayB because elements are not same.
    }else{
       //Don't add ArrayB because elements are same.
    }
}

Try something like this code.

1 Comment

That's a very good answer but it won't cover the case when there are two identical arrays but the arrays inside them are in different order.
0

I'm not sure why this is being done and I'm not sure I completely follow, but you can sort each of the minor arrays before doing any entry by entry comparison. Then the order won't matter.

Comments

0

Maybe you could concat key and value as a new string and compare them together.

Best regards.

/Daniel Carlsson

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.