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.

7
  • 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. Commented Jul 24, 2016 at 9:04
  • 1
    Actually, you aren't at all consistent. Perhaps you left out a frame? Commented Jul 24, 2016 at 9:05
  • 1
    Possible duplicate of How to turn an NSArray of strings into an array of unique strings, in the same order? Commented Jul 24, 2016 at 9:15
  • @FahimParkar I already looked at that answer. It is similar but not a duplicate. Commented Jul 24, 2016 at 9:19
  • @imstillalive : for sure that answer will help you 99%... Commented Jul 24, 2016 at 9:31

3 Answers 3

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"
) ...
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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);

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.