swift Array count Property



Swift does not support the length property to calculate the size of the given array instead it provides the count property to count the total number of elements present in the specified array. It is the most useful property in Array. It is an int-type property, which means the result returned by this property is always of integer type.

For example, we have an array of integer types: Array = [2, 4, 5, 7, 8]. Now using the count property we calculate the size of the array. Hence the size of the array is 5.

Syntax

Following is the syntax of the count property −

var count: Int{get}

Return value

This method returns an integer value that represents the size of the array.

Now we will discuss the usage of the count property with the help of the following examples:

Example 1

Swift program to count the total number of elements present in the given array.

import Foundation

// Array of string type
let array1 = ["Mohan", "Mona"]

// Calculating the size of the array using the count property
let arraySize1 = array1.count
print("Array 1 = \(array1); So the size is:\(arraySize1)")

// Array of integer type
let array2 = [2, 4, 5, 6, 7]

// Calculating the size of the array using the count property
let arraySize2 = array2.count
print("Array 2 = \(array2); So the size is:\(arraySize2)")

// Array of Double type
let array3 = [0.2, 2.4, 33.5, 6.0]

// Calculating the size of the array using the count property
let arraySize3 = array3.count
print("Array 3 = \(array3); So the size is:\(arraySize3)")

// Array of Character type
let array4 = ["D", "M", "V", "S", "Y"]

// Calculating the size of the array using the count property
let arraySize4 = array4.count
print("Array 4 = \(array4); So the size is:\(arraySize4)")

Output

Array 1 = ["Mohan", "Mona"]; So the size is:2
Array 2 = [2, 4, 5, 6, 7]; So the size is:5
Array 3 = [0.2, 2.4, 33.5, 6.0]; So the size is:4
Array 4 = ["D", "M", "V", "S", "Y"]; So the size is:5

Example 2

Swift program to check whether the given array contains elements or not using count property.

import Foundation

// Input 1
// Array of string type
let array1 = ["Car", "Bus", "Books"]

// Calculating the size of the array using the count property
if (array1.count == 0){
    print("Given array is empty")
}else{
    print("Given array contains some elements and the size of the array is \(array1.count)")
}


// Input 2
// Array of integer type
let array2 : [Int] = []

// Calculating the size of the array using the count property
if (array2.count == 0){
    print("Given array is empty")
}else{
    print("Given array contains some elements and the size of the array is \(array2.count)")
}

Output

Given array contains some elements and the size of the array is 3
Given array is empty

Example 3

Swift program to combine two arrays and then calculate the size of the new array.

import Foundation

let arr1 = [3, 5, 6, 2]
let arr2 = [4, 2, 40, 20, 1]

// Combining two arrays
let resultantArray = arr1 + arr2

print("New array = \(resultantArray); New Size = \(resultantArray.count)")

Output

New array = [3, 5, 6, 2, 4, 2, 40, 20, 1]; New Size = 9

Example 4

Swift program to calculate the average of the given array elements.

import Foundation

let nums = [39, 21, 14, 56, 90, 65]
var sum = 0

// Calculate the sum of the array elements
for x in nums{
    sum += x
}

// Calculate the average
let averageOfElements = Double(sum) / Double(nums.count)

// Print the result
print("Average of the given elements: \(averageOfElements)")

Output

Average of the given elements: 47.5
Advertisements