Swift Program to insert multiple elements into the array from the specified index


An array is used to store elements of same data type.

In this article, we will learn how to write a swift program to insert multiple elements into the array from the specified index. Here we use the following methods to insert multiple elements into the array from the specified index −

  • Using Index

  • Using insert(contentsOf:at:) function

Method 1: Using Index

Here we use square brackets[] along with rang operators to insert multiple elements into the array at the specified index.

Algorithm

  • Step 1 − Create an array of integer.

  • Step 2 − Create another array to store new elements.

  • Step 3 − Create and initialise new variable to store the index value.

  • Step 4 − Find the prefixArr and suffixArr of the original array.

  • Step 5 − Concatenate prefixArr, newElements and suffixArr and store the result into new variable.

    let resultArr = prefixArr + newElements + suffixArr

  • Step 6 − Print the output.

Example

Following Swift program to insert multiple elements into the array from the specified index.

import Foundation
import Glibc

// Original Array
var Numbers = [11, 55, 33, 99, 22, 66, 33, 88]

// Array containing new elements 
let newElements = [60, 50, 10]
let index = 4

let prefixArr = Numbers[0..<index]
let suffixArr = Numbers[index..<Numbers.count]

// Adding new elements from the specified index
let resultArr = prefixArr + newElements + suffixArr

print("Original Array: ", Numbers)
print("Updated Array: ", resultArr)

Output

Original Array:  [11, 55, 33, 99, 22, 66, 33, 88]
Updated Array:  [11, 55, 33, 99, 60, 50, 10, 22, 66, 33, 88]

Here in the above code, we have an array of integer, a new array which contains the new elements that we are going to insert in the original array, and the index value at which we insert new elements. Then we create a “prefixArr” which is a subrange of “Numbers” from index 0 to 3. Similarly we create “suffixArr" which is a subrange of “Numbers” from index 4 to the end of the array. Now we concatenate “prefixArr”, “newElements” and “suffixArr” and assign the result to a new variable. So this is how we get an array which has new elements from index 4.

Method 2: Using insert(contentsOf:at:) function

To insert multiple elements into the array from the specified index using insert(contentOf:at:) function. This function is used to insert the elements of a sequence into the array at the specified position.

Syntax

func insert(contentOf: elements, at: IndexValue)

Here, elements is the new elements and IndexValue represent the position at which we insert the new elements. The value of the IndexValue must be a valid index.

Algorithm

  • Step 1 − Create an array of integer.

  • Step 2 − Create another array to store new elements.

  • Step 3 − Create and initialise another variable to store the index value.

  • Step 4 − Insert the new elements at the given index using insert(contentOf:at:) function.

    Numbers.insert(contentsOf: newElements, at: index)

  • Step 5 − Print the output.

Example

Following Swift program to insert multiple elements into the array from the specified index.

import Foundation
import Glibc

// Original Array
var Numbers = [20, 50, 30, 90, 20, 60, 30, 80]

print("Original Array: ", Numbers)

// Array containing new elements 
let newElements = [11, 111, 1111]

// Index from where we want to add new elements
let index = 6

// Adding new elements from the specified index
Numbers.insert(contentsOf: newElements, at: index)

print("Updated Array: ", Numbers)

Output

Original Array:  [20, 50, 30, 90, 20, 60, 30, 80]
Updated Array:  [20, 50, 30, 90, 20, 60, 11, 111, 1111, 30, 80]

Here in the above code, we have an array of integer, a new array which contains the elements that we are going to insert in the original array, and the index value. Now using insert() function we add new elements in the original array from the specified index that is 6.

Conclusion

So this is how we can insert multiple elements into the array from the specified index. In the above methods, insert(contentOf:at:) function modifies the original array. So if you don’t want to modify the original array you can use method 1.

Updated on: 08-Feb-2023

776 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements