Swift Program to Add an Element at the Start Or End of the Array

SwiftServer Side ProgrammingProgramming

This tutorial will discuss how to write swift program to add an element at the start or end of the array.

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 in an array 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 = start

The desired output would be −

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

Suppose our given input is −

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

The desired output would be −

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

Syntax

Following is the syntax to add new element at start of an array −

arrayName.insert(newItem, at: arrayName.startIndex)

Following is the syntax to add new element at end of an array −

arrayName.insert(newItem, at: arrayName.endIndex)

Algorithm

Following is the algorithm −

  • Step 1 − Declare an array with value.

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

  • Step 3 − Insert new element in the array −

At start −

myfruits.insert(newEle, at:myfruits.startIndex)

At end −

myData.insert(newEle, at:myData.endIndex)
  • Step 4 − Print the output

Inserting element at the start of an array

Example

The following program shows how to insert a new element at the start of an array.

import Foundation import Glibc var myfruits : [String] = ["apricot", "grapefruit", "melon"] print("Original Array:", myfruits) // New element var newEle = "pear" print("Element to be added at start:",newEle) // Inserting new element at the // start of the array myfruits.insert(newEle, at:myfruits.startIndex) print("Modified array:", myfruits)

Output

Original Array: ["apricot", "grapefruit", "melon"]
Element to be added at start: pear
Modified array: ["pear", "apricot", "grapefruit", "melon"]

Here, in the above code, we have an array = ["apricot", "grapefruit", "melon"]. Now using insert() function we insert new element(that is "pear") into it at the start or at index 0.

myfruits.insert(newEle, at:myfruits.startIndex)

Hence the output is ["pear", "apricot", "grapefruit", "melon"].

Inserting element at the end of an array

Example

The following program shows how to insert a new element at the end of an array.

import Foundation import Glibc var myData : [Int] = [34, 45, 65, 4] print("Original Array:", myData) // New element var newEle = 1000000 // Inserting new element at the // end of the array myData.insert(newEle, at:myData.endIndex) print("Modified array:", myData)

Output

Original Array: [34, 45, 65, 4]
Element to be added at the end 1000000
Modified array: [34, 45, 65, 4, 1000000]

Here, in the above code, we have an array = [34, 45, 65, 4]. Now using insert() function we add a new element(that is 1000000) into it at end.

myData.insert(newEle, at:myData.endIndex)

Hence the output is [34, 45, 65, 4, 1000000].

raja
Updated on 11-Oct-2022 12:20:27

Advertisements