Arrays are a type of object that is used to store a group of the same type of elements with a fixed size or length.

Swift Array collection type

The array is a data type that stores a group of elements under a single name and memory location. It can be fixed and dynamic collections.

Array features:

  • Values inserted in the array are insertion order
  • Duplicate elements are allowed
  • Used to store the duplicate values with insertion order storage

Performance of the Array operations

OperationDescription
Get An element by index, first, or lasto(1)
Insert an elementAdding at start and end is o(1), Middle is o(n)
delete operationStart and End is o(1) and Middle is o(n)
Containso(n)

How to create an empty array.

There are multiple ways to create an empty blank array.

The first way, using the short-hand syntax of an array of strings.

var array = [String]()

The second way is using the Array type

var array = Array<String>()
var array: Array<String> = Array()

The third way using the empty Array literal.

var array: Array<String> = []
var array: [String] = []

How to add an element to an array?

There are multiple ways we can add an element.

  • Adding elements to the end of an array using append
var words = ["one", "two", "three", "four","five"]
words.append("six")
print(words)

Output:

["one", "two", "three", "four", "five", "six"]
  • Add an element to last using the + operator
var words = ["one", "two", "three", "four","five"]
words+=["six"]
print(words)
["one", "two", "three", "four", "five", "six"]
  • add an element at the array index position
var words = ["one", "two", "three", "four","five"]
words.insert("six",at:2)
print(words)

Output:

["one", "two", "six", "three", "four", "five"]

How to remove an element or object from an array?

There are multiple ways to remove an element from an array.

One way is to mutate an original array, another way is to return a new array without mutating the original array.

The following are ways to mutate an original array.

  • Delete the first element from an array Array removeFirst function removes the first element of an array.
var words = ["one", "two", "three", "four","five"]
words.removeFirst();
print(words)

Output:

["two", "three", "four", "five"]
  • Remove the last element from an array Array removeLast function removes the last element of an array.
var words = ["one", "two", "three", "four","five"]
words.removeLast();
print(words)

Output:

["one", "two", "three", "four"]
  • Remove an element from an array with an index

array remove function index is used to remove an element from an array.

var words = ["one", "two", "three", "four","five"]
words.remove(at: 1)
print(words)

Output:

["one", "three", "four", "five"]
  • Remove all elements from an array Array removeAll function removes all elements of an array and makes an empty array.
var words = ["one", "two", "three", "four","five"]
words.removeAll();
print(words)

Output:

[]

Let’s see an example to return a new array by deleting an element

var words = ["one", "two", "three", "four","five"]
let newwords = words.filter { $0 != "five" }
print(words)
print(newwords)

Output:

["one", "two", "three", "four", "five"]
["one", "two", "three", "four"]

How to find an object or element that exists in an array

Array provides contains function, with where clause checks for the string value. It returns a boolean value true or false. This will be used in a conditional expression.

var words = ["one", "two", "three", "four","five"]
print(words.contains(where: {$0 == "one"})) //true
print(words.contains(where: {$0 == "one1"})) //false
print(words)

Array iterate loop elements in swift

For loop is used to iterate elements of an array in two ways.

One way, using a for loop with the element another way using for loop with an index, and element.

var words = ["one", "two", "three", "four","five"]

print(words)

for element in words {
  print(element)
}

for (index, element) in words.enumerated() {
    print("\(index) : \(element)")
}

Output:

["one", "two", "three", "four", "five"]
one
two
three
four
five
0 : one
1 : two
2 : three
3 : four
4 : five