.union()

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.

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your thoughts in this feedback form.

Learn Swift on Codecademy

Contributors