Home
Map
Set Examples: Insert and ContainsUse the set collection. Insert elements into a set, and use contains and set logic.
Swift
This page was last reviewed on Aug 23, 2023.
Set. This Swift 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.
Here Count returns 3, the number of elements retained in the set. The duplicate element is not counted.
Finally We use the print() func to display the elements present. This is a useful debugging technique.
// 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)
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.
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) }
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.
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) }
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.
Note We call intersection to find the elements that are present in both sets (that intersect). A new set is returned.
Note 2 Subtract() removes elements from one set that are present in another. It modifies the first set in-place.
var 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)
["pink", "red"] ["orange"]
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 23, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.