There is an array with objects

NSArray * array = [NSArray arrayWithObjects:@"1", @"2", @"1", @"4", @"8", @"2", @"2", @"1", @"6", @"1", @"4", nil]; 

The task next is to split this array into smaller arrays with duplicate elements;

Example:

 NSArray * array1 = [NSArray arrayWithObjects:@"1", @"1", @"1", @"1", nil]; NSArray * array2 = [NSArray arrayWithObjects:@"2", @"2", @"2", nil]; NSArray * array3 = [NSArray arrayWithObjects:@"4", @"4", nil]; NSArray * array4 = [NSArray arrayWithObjects:@"6", nil]; NSArray * array5 = [NSArray arrayWithObjects:@"8", nil]; 

p / s I used numbers for example only, in fact objects are compared, and the task is to put identical objects in a separate array.

Implemented through diksheneri

  NSString * stringKey = [dictPrice objectForKey:@"product_id"]; if ([[[SingleTone sharedManager] dictBouquets] objectForKey:stringKey]) { NSMutableArray * array = [[[SingleTone sharedManager] dictBouquets] objectForKey:stringKey]; [array addObject:dictPrice]; } else { NSMutableArray * array = [[NSMutableArray alloc] init]; [array addObject:dictPrice]; [[[SingleTone sharedManager] dictBouquets] setValue:array forKey:stringKey]; } 

But it does not work with an array ... it is necessary to somehow compare the objects of the array not in a loop but in a condition.

  • What have you tried, what did not work out? - Max Mikheyenko
  • if the objects are the same, then you can create a dictionary where the key is an object, and the value how many times the object has already been encountered. and then converted into an array of arrays, simply by copying the object as many times as necessary - Max Mikheyenko
  • Objects are Dixeneri coming from the server, the keys will not be repeated (( - Victor Mishustin
  • so they are the same, or will not be repeated? - Max Mikheyenko
  • They can be repeated, for example, an online store, the client can download any product, you need to set the quantity of goods when you buy, for this you need to sort these goods. - Victor Mishustin

1 answer 1

In order to find duplicate elements, you can use NSCountedSet in any case, you will have to run through the array (ie, in a loop). This is not so expensive operation will have the asymptotics O (n) - for linear time. If the objects are not many.

 NSCountedSet *set = [[NSCountedSet alloc] initWithArray:array]; for (id item in set) { NSLog(@"Name = %@, Count = %@", item, @([set countForObject:item])); } 

But judging by the comments to your question, you just need to get an array of non-repeating elements.

This can be done in several ways:

 // на выходе не отсортированный массив NSArray *uniqueArray = [[NSSet setWithArray:array] allObjects]; // отсортированный массив NSArray *uniqueArray2 = [[NSOrderedSet orderedSetWithArray:array] array]; // не отсортированный массив NSArray *uniqueArray3 = [array valueForKeyPath:@"@distinctUnionOfObjects.self"]; 

If you have a task to know how many goods (the same) come from the server, then the examples above will help. In general, in order to achieve better performance, it is necessary to transfer this concern to the server. He must return the product and its quantity.

ps Use literals when declaring an array: NSArray *array = @[@"1", @"2",...];

  • Thanks, I'll try! - Victor Mishustin