.contains()
Anonymous contributor
Published Oct 23, 2022
Contribute to Docs
The .contains()
method returns true
if the given element exists in the set, false
otherwise.
Syntax
setName.contains(element)
setName
is the set being searched, and element
is the element being searched for.
Example
The following is an example of the .contains()
method:
let numberSet: Set = [1, 2, 3, 4]let targetNumber = 2if numberSet.contains(targetNumber) {print("This set contains this number: \(targetNumber)")} else {print("This set does not contain this number: \(targetNumber)")}
This example results in the following output:
This set contains this number: 2
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Swift on Codecademy
- Skill path
Build iOS Apps with SwiftUI
Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.Includes 7 CoursesWith CertificateBeginner Friendly13 hours - Free course
Learn Swift
A powerful programming language developed by Apple for iOS, macOS, and more.Beginner Friendly12 hours