TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to SWIFT

Swift Set Collection: Insert and Contains

Use the set collection. Insert elements into a set, and use contains and set logic.
Set. This collection allows no duplicate elements. It uses hash codes to test for equality between elements. A set has fast logic tests.
With methods, we perform simple actions like insertion. We can test to see if an element exists. With set operations, we can see if 2 sets intersect.
First program. Here we create an empty set. We then insert() 4 strings into it. The last two strings are the same—only one is retained in the set.

Count: This returns 3, the number of elements retained in the set. The duplicate element is not counted.

Print: We use this func to display the elements present. This is a useful debugging technique.

Swift program that uses set // Create a set and add Strings to it. var animals = Set<String>() animals.insert("cat") animals.insert("bird") animals.insert("dog") animals.insert("dog") // Only unique elements are stored in the set. print(animals.count) // The second dog element is not present. print(animals) Output 3 ["bird", "cat", "dog"]
Remove, contains. We use remove() to erase an element from a set. And with contains() we see whether an element (the argument) exists. It returns true or false.
Swift program that uses remove, contains var knownIds = Set<Int>() knownIds.insert(100) knownIds.insert(200) knownIds.insert(300) // Remove this Int from the set. knownIds.remove(200) // This is contained within the set. if knownIds.contains(100) { print(true) } // Not contained. if !knownIds.contains(200) { print(false) } Output true false
For-in loop. With the for-in loop we can access all the elements in a set. This provides not indexes, but often we do not need indexes.
Swift program that uses for-in loop var languages = Set<String>() languages.insert("Swift") languages.insert("Python") languages.insert("Ruby") // Loop over all elements in the set. // ... Ordering is not maintained. for language in languages { print(language) } Output Python Ruby Swift
Intersection, subtract. This program first initializes sets with initializer syntax. This is an easier way to create a new set from known element.

Intersection: We call intersection to find the elements that are present in both sets (that intersect). A new set is returned.

Subtract: This removes elements from one set that are present in another. It modifies the first set in-place.

Swift program that uses set initializer, intersect, subtract let colors1: Set<String> = ["red", "orange", "pink"] let colors2: Set<String> = ["blue", "red", "pink"] // Intersection returns a set of the two shared Strings. // ... Red and pink are shared. let intersection = colors1.intersection(colors2) print(intersection) // Subtract removes shared values. // ... Red and pink are removed in the set. // ... The colors1 set is modified in-place. colors1.subtract(colors2) print(colors1) Output ["pink", "red"] ["orange"]
A summary. Sets have many uses in programs. They help with simple requirements, like ensuring uniqueness. More advanced things, like set operations, are less often needed.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf