Swift Array - isEmpty Property



The isEmpty property of the Array is used to check whether the given array is empty or not. It is the most commonly used property of Array. It checks whether the given collection is empty or not instead of checking the size of the property. The complexity of this property is O(1).

For example, we have an array of integer types: Array = [2, 4, 5]. Now using isEmpty property we check if the array is empty or not. So this property returns false which means the given array is not empty. If the array is empty, then it will return true.

Syntax

Following is the syntax of the isEmpty property −

var isEmpty: Bool{get}

Return Value

This method returns a boolean value. If the given array is empty, then it will return true. Or if the given array contains some value, then it will return false.

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

Example 1

Swift program to check if the given array is empty or not with the help of the isEmpty property.

import Foundation
// Input array of integer type
let arr: [Int] = []

// Checking if the given array is empty or not
if arr.isEmpty == true {
   print("Yes the given array is empty")
} else {
   print("No the given array is not empty")
}

Output

Yes the given array is empty

Example 2

Swift program to check how the isEmpty property works with different data types.

import Foundation
// Input array of integer type
let arr1 = [3, 5, 6, 2]

// Input array of string type
let arr2 = ["Mohan", "Sumita", "Sonalika"]

// Input array of double type
let arr3 = [4.5, 6.7, 88.8]

// Checking if the given arrays are empty or not using isEmpty
print("Is arr1 is empty:", arr1.isEmpty)
print("Is arr2 is empty:", arr2.isEmpty)
print("Is arr3 is empty:", arr3.isEmpty)

Output

Is arr1 is empty: false
Is arr2 is empty: false
Is arr3 is empty: false

Example 3

Swift program to find odd numbers from the given array. Here we first check if the given array is empty or not using isEmpty. If the array is empty, then this function will return nil, otherwise return the array with contains only odd numbers.

import Foundation

// Function to find odd numbers
func oddNums(arr: [Int]) -> [Int]? {

   // Checking if the given array is empty or not
   if arr.isEmpty == true {
      return nil
   }

   // Finding the odd numbers
   let oddNumbers = arr.filter { $0 % 2 != 0 }
   return oddNumbers
}

let inputArr = [3, 6, 1, 9, 7]

if let result = oddNums(arr: inputArr) {
   if result.isEmpty {
      print("Array is empty")
   } else {
      print("Odd numbers are: \(result)")
   }
} else {
   print("Array is empty")
}

Output

Odd numbers are: [3, 1, 9, 7]

Example 4

Swift program to find the mean of the array elements. Here we first check if the given array is empty or not using the isEmpty property. If the given array is empty, then the function will return nil. Otherwise, it will return the final mean.

import Foundation

// Function to calculate the mean of the given array
func calculateMean(arr: [Int]) -> Double? {

   // If the given array is empty return nil
   guard !arr.isEmpty else {
      return nil
   }

   // Calculating the sum of the array elements
   let sum = arr.reduce(0, +)

   // Calculating the mean
   let mean = Double(sum / arr.count)
   return mean
}

let inputs = [30, 45, 32, 10, 19, 40]
if let result = calculateMean(arr: inputs) {
   print("Mean: \(result)")
} else {
   print("Cannot calculate mean because array is empty.")
}

Output

Mean: 29.0
Advertisements