.contains()

The .contains() method returns true if an array contains a specified element, false otherwise.

Syntax

arrayName.contains(element)

Example

var topBabyNames = ["Sophia", "Liam", "Riley", "Jackson", "Olivia", "Noah"]
print(topBabyNames.contains("Olivia"))
// Output: true

Note: Big-O of this method is O(n).

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Swift on Codecademy