-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.
Tony TRAN
  • 2,118
  • 1
  • 15
  • 16
  • 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. – rmaddy Jun 10 '16 at 04:39
  • possible duplicate of http://stackoverflow.com/questions/1025674/the-best-way-to-remove-duplicate-values-from-nsmutablearray-in-objective-c – Prashant Tukadiya Jun 10 '16 at 04:41
  • Thanks, rmaddy. This question is not duplicate from that question, Mike. – Tony TRAN Jun 10 '16 at 04: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. – rmaddy Jun 10 '16 at 04:49
  • is your subarray going to be mutable or immutable? – Saheb Roy Jun 10 '16 at 05:29
  • All sub arrays is mutable, Saheb. – Tony TRAN Jun 10 '16 at 05:31

4 Answers4

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))
Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
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);
Fawad Masud
  • 12,219
  • 3
  • 25
  • 34
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];
 }
Paulw11
  • 108,386
  • 14
  • 159
  • 186
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

Iyyappan Ravi
  • 3,205
  • 2
  • 16
  • 30