1

I am very new to the Objective-C language and I was asked a question in the interview that I was not able to answer.

I tried a lot of approaches and logic to no success. Here looking for an answer:

The question was:

 - (NSArray *)reorderTheArraysAndMergeThemInDescendingOrder:(NSArray *)firstArray and:(NSArray *)secondArray

Both the provided arrays in the argument are already sorted in ascending order.

My task was to provide an array as the return variable which will have all the unique elements of both firstArray and the secondArray and will have them in theh descending order. I was not allowed to use the inbuilt sorting functionality of Objective-C (that was the way I have been doing it ever since I am in this field). My variables may only be primitives (the result will be an integer array of course.).

I am new to the whole programming scene altogether and a good answer here will be MUCH appreciated.

Shukaku

Alexander
  • 8,117
  • 1
  • 35
  • 46
Anton Unt
  • 1,835
  • 1
  • 21
  • 47
  • For Uniques elements in array check this:- http://stackoverflow.com/questions/1439564/iphone-getting-unique-values-from-nsarray-object For Sorting array in descending order:- http://stackoverflow.com/questions/3402667/sort-an-nsarray-in-descending-order – Leena Jul 23 '12 at 06:12

2 Answers2

5

Since the provided arrays are sorted in ascending order, this question is pretty-much related to the classic algorithm of Mergesort (have a look at it if you've never heard of it, this is a classic).

Since you're array contain only primitive data types, they can be compared using <

The code that interest you would look like

- (NSArray *)reorderAndMergeReverse:(NSArray *)array1 and:(NSArray *)array2{

    NSMutableArray *result = [NSMUtableArray array];

     int i = array1.count-1;
     int j = array2.count-1;

     while (result.count < array1.count+array2.count){

        if ([array1 objectAtIndex:i] > [array2 objectAtIndex:j]){
             [result addObject:[array1 objectAtIndex:i]];
             i--:
        } else {
            [result addObject:[array2 objectAtIndex:j]];
             j--:
        }

     }
     return result;
}

I am not sure as to whether you want to remove duplicates, your question is not that clear, would you mind telling me if so so that i can improve my code ?
Edit : Did it anyway : since you're array is sorted, duplicates are just next to each other, so it might be easy not to add them in the first place;

- (NSArray *)reorderAndMergeReverse:(NSArray *)array1 and:(NSArray *)array2{

    NSMutableArray *result = [NSMUtableArray array];

     int i = array1.count-1;
     int j = array2.count-1;
     int k = 0;

     while (i>=0 || j>=0){

        if ([array1 objectAtIndex:i] > [array2 objectAtIndex:j]){
             if ([array1 objectAtIndex:i] != [result objectAtIndex:k]){
                 [result addObject:[array1 objectAtIndex:i]];
                 k++;
             }
             i--:
        } else {
            if (array2 objectAtIndex:j] != [result objectAtIndex:k]){
                   [result addObject:[array2 objectAtIndex:j]];
                   k++;
             }
             j--:
        }

     }
     return result;
}

Edit : There are some errors in that code, because i could become negative and afterward be used in objectAtIndex:, this is just meant to demonstrate the idea, not to give an out-of-the-box code solution

Olotiar
  • 3,225
  • 1
  • 18
  • 37
  • Thank you for your answer, I am taking clues from it. Yes, the duplicates should not be there. For example, if arrayfirst has 2,3,4 and the secondArray has 3,4,5... the resultant array should be 5,4,3,2. – Anton Unt Jul 23 '12 at 06:31
  • Just edited my post to remove duplicates (mmore precisely not to add them in the first place) – Olotiar Jul 23 '12 at 06:32
1

There are many ways to approach this. You could reverse the arrays, then insert items from the second into the first, or you could append the second to the first then resort them.

I will post code later demonstrating the simplest way I can find. How fast and efficient does it have to be? Can it be anything as long as it gets the job done?

Edit: It appears that Olotiarhas beat me to it.

David Skrundz
  • 13,067
  • 6
  • 42
  • 66