0

I have two mutable arrays:

NSMutableArray array1; [containing 1,2,3,4,5]

NSMutableArray array2; [containing 9,8,7]

I want to insert array2 values to array1 between 3 and 4, so the result could be:

array1 = [1,2,3,9,8,7,4,5]

Again I want to remove array2 values from array1, so the result could be:

array1 = [1,2,3,4,5]

Please suggest me some best approach to achieve it.

The below links doesn't help: Copy object at specific index of mutable array to the end of another array

5
  • its fix that you want to insert array2 at 4th index into array1? Commented Mar 1, 2017 at 17:52
  • No, at any index I can insert array2, but yes at one time, it will be given the index value. Commented Mar 1, 2017 at 17:54
  • @Ren You need to add object after object 3 that fix right? Means always between object 3 and 4. Commented Mar 1, 2017 at 17:57
  • Yes it is given Commented Mar 1, 2017 at 17:58
  • I'm not sure what you're trying to do. You need to add array2 and then remove it? Isn't the result just going to be the same as array1's initial value every time? Commented Mar 1, 2017 at 18:33

2 Answers 2

3

Based on your requirement you can split your first array into 2 part and merge all 3 array to final array for getting expected output.

Please check this code:

    NSMutableArray *mutArray1 = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];
    NSMutableArray *mutArray2 = [[NSMutableArray alloc] initWithObjects:@"9",@"8",@"7", nil];

    //You cna change index based on your requirement.
    int indexToSpit = 3;

    //Split your first array into 2 part.
    NSArray *arayFirstPart = [mutArray1 subarrayWithRange:NSMakeRange(0, indexToSpit)];
    NSArray *araySecondPart = [mutArray1 subarrayWithRange:NSMakeRange(indexToSpit, mutArray1.count-indexToSpit)];

    //Merge all 3 array into single array
    NSMutableArray *finalArray = [[NSMutableArray alloc] initWithArray:arayFirstPart];
    [finalArray addObjectsFromArray:mutArray2];
    [finalArray addObjectsFromArray:araySecondPart];

    NSLog(@"Combine Array : %@",finalArray);

    //For remove
    [finalArray removeObjectsInArray:mutArray2];

    NSLog(@"Split Array : %@",finalArray);

Hope this will helps you.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! lemme check it.
0
var array1 =  [1,2,3,4,5]
var array2 =  [9,8,7]

for inserting array2 at index 3 use

array1.insertContentsOf(array2, at: 3)
print(array1)

for removing you can either use

array1 = Array(Set(array1).subtract(array2))

which will be unordered array as we are converting a Array to Set for subtraction.

or you can try

    var array3 =  [1,2,3,4,5]
       //assuming array1 as [1,2,3,9,8,7,4,5]
    let array4 = array1.filter({array3.contains($0)}) 
    print(array4) //will give you desired ordered array

1 Comment

Thanks jagdeep, but i am using objective C.

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.