Home
Map
Set Collection: Insert and ContainsUse the set collection. Insert elements into a set, and use contains and set logic.
Swift
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.
Detail This returns 3, the number of elements retained in the set. The duplicate element is not counted.
Detail We use this 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.
Detail We call intersection to find the elements that are present in both sets (that intersect). A new set is returned.
Detail This removes elements from one set that are present in another. It modifies the first set in-place.
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)
["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.
C#VB.NETPythonGolangJavaSwiftRust
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.
No updates found for this page.
Home
Changes
© 2007-2023 Sam Allen.