Swift – Create a Set from Array Literal

To create a Set from the values of an Array literal in Swift, declare a Set variable and initialize it with Array literal.

The syntax to create a Set x from an Array literal is

</>
Copy
var x: Set = [value1, value2]

Examples

In the following program, we will create a Set from an array of integers.

main.swift

</>
Copy
var x: Set = [25, 4, 9]
print(x)

Output

Swift - Create a Set from Array Literal - Example

Conclusion

In this Swift Tutorial, we have learned how to create a Set from Array literal, in Swift programming.