0

I have 2 arrays:

Array one: (@"26", @"26", @"25", @"25", @"3", @"3", @"4", @"4")

Array two: (@"sticker", @"sticker", @"sticker", @"sticker", @"sticker", @"frame", @"frame", @"frame")

Edit

These 2 arrays are connected like this:

26 - sticker
26 - sticker
25 - sticker
25 - sticker
3 - frame
3 - frame
4 - frame
4 - frame

I'm getting unique value in array one and putting it in another array like this:

NSArray *uniqueArrayOne = [[NSSet setWithArray:arrayOne] allObjects];

So uniqueArrayOne looks like this:

(26, 25, 3, 4)

I want uniqueArrayTwo to also be arrange like how uniqueArrayOne was arranged. I want uniqueArrayTwo to look like this:

(sticker, sticker, frame, frame)

What do I do?

Edit

Here is another example:

Array one: (@"TONY", @"SANSA", @"BILL", @"BILL", @"STEVE", @"STEVE")

Array two: (@"STARK", @"STARK", @"GATES", @"GATES", @"JOBS", @"JOBS")

The results should be like this:

uniqueArrayOne :(TONY, SANSA, BILL, STEVE)

uniqueArrayTwo :(STARK, STARK, GATES, JOBS)

uniqueArrayTwo is arrange depending on uniqueArrayOne.

Sam B
  • 27,273
  • 15
  • 84
  • 121
imstillalive
  • 369
  • 4
  • 14
  • You have to clarify that you want the second instance of each number form the first array. It's not at all obvious from the question. – Avi Jul 24 '16 at 09:04
  • 1
    Actually, you aren't at all consistent. Perhaps you left out a `frame`? – Avi Jul 24 '16 at 09:05
  • 1
    Possible duplicate of [How to turn an NSArray of strings into an array of unique strings, in the same order?](http://stackoverflow.com/questions/4209823/how-to-turn-an-nsarray-of-strings-into-an-array-of-unique-strings-in-the-same-o) – Fahim Parkar Jul 24 '16 at 09:15
  • @FahimParkar I already looked at that answer. It is similar but not a duplicate. – imstillalive Jul 24 '16 at 09:19
  • @imstillalive : for sure that answer will help you 99%... – Fahim Parkar Jul 24 '16 at 09:31
  • I agree with @fahim-parkar, the answers on that question get you most of the way there. (I wouldn't say quite 99%.) – Avi Jul 24 '16 at 09:49
  • Why don't you use a `NSArray` of `NSDictionary` instead of `arrayOne` and `arrayTwo` => `NSArray *array = @[@{@"Name:"@"Tony", @"FamilyName":@"Stark"},@{@"Name:"@"Sansa", @"FamilyName":@"Stark"}, @{@"Name:"@"Bill", @"FamilyName":@"Gates"} ]`? You are trying to synchronize the items on two separates array where putting it in only may be clearer. – Larme Jul 25 '16 at 12:12

3 Answers3

1

I would solve it this way

- (void)uniqueArrayFinder
{
    NSArray *array1 = [[NSArray alloc] initWithObjects:
                       @"TONY", @"SANSA", @"BILL", @"BILL", @"STEVE", @"STEVE",nil];
    NSArray *array2 = [[NSArray alloc] initWithObjects:
                       @"STARK", @"STARK", @"GATES", @"GATES", @"JOBS", @"JOBS",nil];

    NSMutableArray *combinedArray = [[NSMutableArray alloc] init];

    for (int i=0; i<[array1 count];i++)
    {
        NSString *arrayVal1 = [array1 objectAtIndex:i];
        NSString *arrayVal2 = [array2 objectAtIndex:i];
        NSString *combinedStr = [NSString stringWithFormat:@"%@ %@", arrayVal1, arrayVal2];
        [combinedArray addObject:combinedStr];
    }

    //this gives uniqe values
    NSSet *uniqueEvents = [NSSet setWithArray:combinedArray];
    [combinedArray removeAllObjects];
    [combinedArray addObjectsFromArray:[uniqueEvents allObjects]];

    NSLog(@"combinedArray: %@ ...", combinedArray);


}

Output:

combinedArray: (
    "STEVE JOBS",
    "TONY STARK",
    "SANSA STARK",
    "BILL GATES"
) ...
Sam B
  • 27,273
  • 15
  • 84
  • 121
1

You can achieve the desired result with the following,

NSArray *arr1 = @[@"26", @"26", @"25", @"25", @"3", @"3", @"4", @"4"];
NSArray *arr2 = @[@"sticker", @"sticker", @"sticker", @"sticker", @"sticker", @"frame", @"frame", @"frame"];

NSDictionary *dict = [NSDictionary dictionaryWithObjects:arr2 
                                   forKeys:arr1];

NSArray *distinctArr1 = [[NSOrderedSet orderedSetWithArray:arr1] array];

NSMutableArray *distinctArr2 = [NSMutableArray array];
for (NSString *num1 in distinctArr1) {
    [distinctArr2 addObject:dict[num1]];
}
// Your distinctArr2 is sorted based on distinctArr1's indices
ldindu
  • 4,270
  • 2
  • 20
  • 24
0

Try this one.

NSArray *arrayOne = @[@"TONY", @"SANSA", @"BILL", @"BILL", @"STEVE", @"STEVE"];
NSArray *arrayTwo = @[@"STARK", @"STARK", @"GATES", @"GATES", @"JOBS", @"JOBS"];

NSArray *uniqueArrayOne = [[NSSet setWithArray:arrayOne] allObjects];
NSMutableArray *uniqueArrayTwo = [NSMutableArray array];

[uniqueArrayOne enumerateObjectsUsingBlock:^(id  _Nonnull obj1, NSUInteger idx, BOOL * _Nonnull stop) {
    NSUInteger found = [arrayOne indexOfObjectPassingTest:^BOOL(id  _Nonnull obj2, NSUInteger idx, BOOL * _Nonnull stop) {
        return [(NSString *)obj2 isEqualToString:(NSString *)obj1];
    }];
    [uniqueArrayTwo addObject:[arrayTwo objectAtIndex:found]];
}];

NSLog(@"%@, %@", uniqueArrayOne, uniqueArrayTwo);
jp2g
  • 682
  • 4
  • 8