.union()
Published Oct 24, 2022
Contribute to Docs
The .union()
method returns a new set containing all elements of one set combined with the elements of a given set.
Syntax
firstSet.union(secondSet)
Sets firstSet
and secondSet
must have countable number of members.
Example
let friends: Set = ["Pam", "Jim", "Andy", "Darryl"]let colleagues = ["Dwight", "Michael", "Ed"]let friendsAndColleagues = friends.union(colleagues)print(friendsAndColleagues)let luckyNumbers: Set = [12, 13]let luckySequence = (4...8)let goodNumbers = luckyNumbers.union(luckySequence)print(goodNumbers)
This will output:
["Darryl", "Pam", "Jim", "Ed", "Andy", "Michael", "Dwight"][12, 4, 5, 6, 7, 8, 13]
Note: A set is an unordered data structure, so output elements may be ordered differently.
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.