Swift Program to Insert an Element in an Array at a Specified Index

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to insert an element in an array at a specified index.

An array is an ordered collection which is used to store same type of data. For example, if any array is of integer type then it will only store integers, you are strictly not allowed to store elements of other data types like string, float, etc.

In an array, we can insert a new element at a specified index using insert() function. It is an in-built function. In this function, we just need to pass two arguments that are element and index and it will insert in the array. It does not return any value it just modifies the original array..

While inserting element in the array always remember the following points −

The type of new element and the array must be same.

the index must be within the index limit of the given array. Otherwise you will get error.

Array must be mutable.

Below is a demonstration of the same −

Suppose our given input is −

MyArray = ["summer", "winter", "spring"]
New Element = "autumn"
Index = 1

The desired output would be −

Updated array = ["summer", "autumn", "winter", "spring"]

Syntax

Following is the syntax −

arrayName.insert(newItem, at: indexVal)

Algorithm

Following is the algorithm −

  • Step 1 − Declare an array with value.

  • Step 2 − Declare and assign a variable with new element.

  • Step 3 − Declare and assign another variable index.

  • Step 4 − Insert new element at specified index using insert() function.

  • Step 5 − Print the output.

Example 1

The following program shows how to insert an element in an array at a specified index.

import Foundation import Glibc var myNum : [Int] = [34, 9, 23, 2, 98, 453] print("Original Array:", myNum) // New element var newEle = 3453 // Index at which we insert new element var myIndex = 3 // Inserting new element at index 3 myNum.insert(newEle, at: myIndex) print("Modified array:", myNum)

Output

Original Array: [34, 9, 23, 2, 98, 453]
Modified array: [34, 9, 23, 3453, 2, 98, 453]

Here, in the above code, we have an array = [34, 9, 23, 2, 98, 453]. Now using insert() function we insert new element(that is 3453) into it at index 3:Here, in the above code, we have an array = [34, 9, 23, 2, 98, 453]. Now using insert() function we insert new element(that is 3453) into it at index 3 −

myNum.insert(newEle, at: myIndex)

Hence the output is [34, 9, 23, 3453, 2, 98, 453].

Example 2

The following program shows how to insert an element in an array at a specified index.

import Foundation import Glibc var myVeggis : [String] = ["corn", "broccoli", "green beans", "spinach"] print("Original Array:", myVeggis) // New element var newEle = "radish" print("Element t the added =",newEle) // Index at which we insert new element var myIndex = 1 // Inserting new element at index 1 myVeggis.insert(newEle, at: myIndex) print("Modified array:", myVeggis)

Output

Original Array: ["corn", "broccoli", "green beans", "spinach"]
Element t the added = radish
Modified array: ["corn", "radish", "broccoli", "green beans", "spinach"]

Here, in the above code, we have an array = ["corn", "broccoli", "green beans", "spinach"]. Now using insert() function we insert new element(that is "radish") into it at index 1 −

myVeggis.insert(newEle, at: myIndex)

Hence the output is ["corn", "radish", "broccoli", "green beans", "spinach"].

raja
Updated on 11-Oct-2022 12:18:15

Advertisements