swift Array contains() Function



The contains() function is a predefined array function in Swift. It is used to check whether the given element is present in the specified array or not. It is the most commonly used method to check the availability of the element in the given array. Its return result is a boolean value either true or false. It can work well with both mutating and non-mutating arrays.

For example, we have an array = [30, 40, 50, 60]. Now with the help of the contains(40) function we will check if the given array contains element = 40 or not. So the result is true which means the given array contains 40.

We can use the contains() function in two different ways −

  • contains(_:) function
  • contains(where:) function

Swift Array contains(_:) Function

The contains() function is used to check whether the specified element is present in the given array or not. The complexity of this function is O(n), where n represents the length of the array.

Syntax

Following is the syntax of the contains(_:) function −

func contains(_element:Self.Element) -> Bool

Parameters

This function takes only one parameter that is element. Here element represents the element that we want to check. If it is present in the array or not.

Return value

This function returns true if the element is available in the array. Otherwise, return false.

Now we will discuss how to use the contains() function with the help of the following examples:

Example 1

Swift program to check whether the given element is present in the array or not using the contains() function.

import Foundation

// Defining an array of integer type
var array = [4, 3, 2, 1, 11, 10, 19]

// Checking if the given element is present or not
let res1 = array.contains(1)
let res2 = array.contains(11)
let res3 = array.contains(23)
let res4 = array.contains(30)

print("Is 1 present in the array?:", res1)
print("Is 11 present in the array?:", res2)
print("Is 23 present in the array?:", res3)
print("Is 30 present in the array?:", res4)

Output

Is 1 present in the array?: true
Is 11 present in the array?: true
Is 23 present in the array?: false
Is 30 present in the array?: false

Example 2

Swift program to remove the first element from the given array only if the given condition is true.

import Foundation

// Defining an array of string type
var pNames = ["Swift", "C", "Java", "Perl"]
let element = "Swift"

// Checking if the given element is present or not
// Using contains() function
if (pNames.contains(element)){
    print("YES! the given array contains '\(element)'")
}else{
    print("NO! the given array does not contains '\(element)'")
}

Output

YES! the given array contains 'Swift'

Swift Array contains(where: ) Function

The contains(where:) function is used to check whether the given array contains the elements that satisfy the given condition present in the closure or not. The complexity of this function is O(n), where n represents the length of the array.

Syntax

Following is the syntax of the contains(where:) function −

func contains(where mClosure: (Self.Element) throws -> Bool) rethrows -> Bool

Parameters

Here mClosure takes each element from the given array as an argument and returns a boolean value that represents whether the passed element satisfies the given condition or not.

Return value

This function returns true if the given array contains the element that satisfies the given condition. Otherwise, return false.

Now we will discuss how to use the contains(where:) function with the help of the following examples:

Example 1

Swift program to check whether the given array contains the specified element that satisfies the given condition using the contains() function.

import Foundation

// Electricity bill amount of last 6 months
var electricityBillAmount = [2340, 2560, 2200, 4505, 6700, 5500]

// Checking if the amount is greater than 5000 or not
// Using contains(where:) function
let amount = electricityBillAmount.contains { $0 > 5000 }

print("Is there any amount greater than 5000?: \(amount)")

Output

Is there any amount greater than 5000?: true

Example 2

Swift program to check if the given array contains any odd number or not using the contains() function.

import Foundation

// Array
let nums = [20, 54, 78, 18, 10, 12]

// Check if the array contains any odd numbers or not
// Using contains(where:) function
let oddNums = nums.contains(where: { $0 % 2 != 0 })

if (oddNums == true) {
    print("Given array contains odd numbers")
} else {
    print("Given array does not contain any odd number")
}

Output

Given array does not contain any odd number
Advertisements