-1

I'm looking for the best solution to remove duplicate objects from multi dimension array in objective-C (Swift is also fine) from some array like this:

muliDemensionArray = @[
                                 @[@"1", @"2", @"3", @"4", @"4",],
                                 @[@"11", @"13", @"24", @"14",],
                                 @[@"1", @"3", @"24", @"21",],
                                 ];

Do we have any algorithm or solution from NSOrderedSet/NSMutableArray support us to do this without loop/ reduce loop as much as possible?

This is expected result to remove all duplicates across all arrays:

mutilDemensionArray = @[
                                 @[@"1", @"2", @"3", @"4",],
                                 @[@"11", @"13", @"24", @"14",],
                                 @[@"21",],
                                 ];
  • If we have many duplicate object, so keep the first one and remove others.
  • I don't care about the order of objects in sub arrays, just care about the order of the sub arrays.
6
  • 1
    You need to clarify the goal. Do you want duplicates removed from each inner array or all duplicates across all arrays? Update your question with the expected output and show what you have tried so far. Commented Jun 10, 2016 at 4:39
  • possible duplicate of stackoverflow.com/questions/1025674/… Commented Jun 10, 2016 at 4:41
  • Thanks, rmaddy. This question is not duplicate from that question, Mike. Commented Jun 10, 2016 at 4:46
  • 1
    Again, show what you have already tried. And explain how you came to that result. Why was the "1" removed from the first array and not the third, for example. Commented Jun 10, 2016 at 4:49
  • is your subarray going to be mutable or immutable? Commented Jun 10, 2016 at 5:29

4 Answers 4

1

You can use the property of NSSets that they will only store a single instance of a given value.

  • First convert each sub-array into a set, that will remove duplicates that are in that array.

  • Then, subtract the set of items that have already been seen.

  • Convert the result back to an array and add it to your output array.

  • Finally, add the items in that sub-array to the set of items that have already been seen.

Something like:

-(NSArray *)removeDuplicatesFromArray:(NSArray *)array {

    NSMutableArray *returnArray=[NSMutableArray new];
    NSMutableSet *cumulativeSet=[NSMutableSet new];

    for (NSArray *innerArray in array) { 
        NSMutableSet *innerSet = [NSMutableSet setWithArray:innerArray];
        [innerSet minusSet:cumulativeSet];
        [cumulativeSet unionSet:innerSet];

        [returnArray addObject:[innerSet allObjects]];
    }

    return [returnArray copy];
 }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it really help!
1

Please try this and say if this solves your purpose

 NSArray *multidimentionalArray = @[
                                 @[@"1", @"1",@"2",@"3",@"2"],
                                 @[@"90", @"91",@"92",@"90",@"92"]];
NSMutableArray *mutableCopy = [multidimentionalArray mutableCopy];
for (NSArray *arr in multidimentionalArray) {
    NSSet *s = [NSSet setWithArray:arr];
    [mutableCopy removeObject:arr];
    [mutableCopy addObject:[s allObjects]];
}
multidimentionalArray = mutableCopy;
NSLog(@"%@",multidimentionalArray);
//(( 3,1,2), (92,91,90))

2 Comments

Thank you, but remove duplicate objects in cross array is what I'm looking for.
you cannot remove from an array/structure while you are enumerating
1

Please try the following code. I have tested it.

   NSArray *muliDemensionArray = @[
                                @[@"1", @"2", @"3", @"4", @"4", @"6", @"7", @"7", @"9", @"10", @"11", @"12"],
                                @[@"11", @"13", @"24", @"14", @"16", @"16", @"17", @"18", @"19", @"20", @"21", @"22"],
                                @[@"1", @"3", @"24", @"21", @"31", @"312", @"412"],
                                @[@"23", @"42", @"32", @"41", @"424", @"55", @"123", @"54", @"123"],
                                @[@"132", @"123", @"123", @"412", @"41", @"1", @"2", @"4", @"5", @"6", @"31"],
                                ];


NSMutableArray *newArray = [NSMutableArray new];
NSMutableArray *tempArr = [NSMutableArray new];

for (int i = 0; i<muliDemensionArray.count;i++) {

        NSArray *indexArray = [muliDemensionArray objectAtIndex:i];

        NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:indexArray];
        indexArray = [orderedSet array];

        NSMutableArray *arr1 = [NSMutableArray new];

        for (NSString *str in indexArray) {
            if (![tempArr containsObject:str]) {

                [arr1 addObject:str];

            }
        }
        [tempArr addObjectsFromArray:indexArray];
        [newArray addObject:arr1];


}

NSLog(@"%@",newArray);

1 Comment

Thank you. It work fine however we have to run loop doubles.
1

If your Array is NSArray variable use this code,

NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:mutilDemensionArray];
mutilDemensionArray = [orderedSet array];

If your Array is NSMutableArray variable use this single line code,

[mutilDemensionArray addObjectsFromArray:[[NSSet setWithArray: mutilDemensionArray] allObjects]];

hope its helpful

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.