Swift Sets Collection Type

In swift, sets are used to store same type of distinct values in an unordered manner. We can use sets instead of arrays in case if we want to store only distinct values and we are not concerned about the order of items. 

 

Following is the pictorial representation of sets in swift programming language.

 

Swift Set Collection Types with Examples

 

If you observe above image we are storing different values in swift sets in unordered manner.

 

In swift we can create sets same arrays like surround a list of comma-separated values with square brackets except by specifying Set type.

Syntax of Swift Sets

Following is the syntax of defining set collection types in the swift programming language.

 

Set<ElementType>

Here ElementType is used to specify data type of values which we are going to store in sets.

Create a Sets in Swift

The following are the different ways of defining or declaring sets in swift programming language.

 

One way of defining sets, elements with comma-separated values surrounded by square brackets along with Set type specified.

 

// Set of integer elements

var numbers: Set  = [1, 2, 3, 4]

 

// Set of string elements

var names: Set = ["Suresh", "Rohini", "Praveen", "Sateesh"]

Following are the different ways to create sets with data type in set declaration.

 

var nums: Set<Int> = [1,2,3]

print(nums)

// It prints [1, 2, 3]

 

var names: Set<String> = ["Suresh", "Rohini", "Praveen", "Sateesh"]

print(names)

// It prints [Suresh, Rohini, Praveen, Sateesh]

Following is the one way of creating an empty sets with data type in sets declaration.

 

let nums = Set<Int>()

print(nums.count)

// It prints 0

Now we will see how to access set elements in swift programming language with examples.

Accessing Set Elements

In swift, we can access Set elements by using some of the built-in methods like contains, first, etc.

 

Following is the example of checking whether Set contains particular element or not using contains() method and get the first element in Set using the first property.

 

var names = ["suresh", "rohini", "praveen", "sateesh"]

if names.contains("suresh") {

print("Set Contains Element")

} else {

print("Element Not Exists")

}

If you observe above example we are checking Set type contains particular elements or not using contains method.

 

When we run the above program in the swift playground that will return a result like as shown below

 

Set Contains Element

This is how we can check whether the given Set contains elements or not in swift programming language based on our requirements.

 

We can access first element of Set using first property in swift programming language.

 

Following is the example of using first property to access Set elements.

 

var names: Set = ["suresh", "rohini", "praveen", "sateesh"]

print(names.first)

When we run above program in swift playground that will return result like as shown below.

 

suresh

This is how we can access the first element of Set in a swift programming language based on our requirements.

Check Set Empty or Not

By using isEmpty property we can check whether the given set contains elements or not in swift programming language.

 

Following is the example of using isEmpty property to check whether the given set contains elements or not.

 

var names: Set = ["suresh", "rohini", "praveen", "sateesh"]

if names.isEmpty  {

print("Set is Empty")

} else {

print("Set Contains Elements")

}

If you observe above example we are checking whether set contains elements or not using isEmpty property.

 

When we run above program in swift playground that will return a result like as shown below

 

Set Contains Elements

This is how we can check whether the given set contains elements or not in swift programming language based on our requirements.

Get Set Elements Count

In swift by using count property we can get the count or number of elements exists in sets.

 

Following is the example of using count property to get number of elements exists in sets.

 

var names: Set = ["suresh", "rohini", "praveen", "sateesh"]

if names.count > 0 {

print("Set Contains \(names.count) Elements")

} else {

print("No Elements")

}

If you observe above example we are checking set elements count using count property.

 

When we run above program in swift playground that will return result like as shown below

 

Set Contains 4 Elements

This is how we can get the number of elements that exists in sets in a swift programming language based on our requirements.

Add Elements to a Set

In swift we can insert or add elements to a set by using the insert() method.

 

Following is the example of inserting new elements in Set using insert() method in swift programming language.

 

var names: Set = ["suresh", "rohini", "praveen", "sateesh"]

names.insert("madhav")

print(names)

If you observe above example we are inserting new element in Set using insert() method.

 

When we run above program in swift playground that will return result like as shown below

 

[madhav, suresh, rohini, praveen, sateesh]

This is how we can insert or add elements to an existing Set in swift programming language based on our requirements.

Remove Set Elements

In Swift, we can easily delete or remove the elements from Set by using remove() method.

 

Following is the example of removing elements in Set by using remove() method.

 

var names: Set = ["suresh", "rohini", "praveen", "sateesh"]

names.remove("praveen")

print(names)

If you observe above example we are deleting element in Set by using “remove” method.

 

When we run above example we will get a result like as shown below

 

[suresh, rohini, praveen]

This is how we can delete elements in Set based on our requirements in swift programming language.

Iterating Over a Set Elements

By using for-in loop we can iterate or loop through elements in sets based on our requirements in swift programming language.

 

Following is the example of iterating over an elements in set by using the for-in loop.

 

var names: Set = ["suresh", "rohini", "praveen", "sateesh"]

for name in names {

print(name)

}

When we run above example we will get result like as shown below.

 

suresh

rohini

praveen

sateesh

In swift, Set type elements don’t have any defined order. We can iterate over Set of elements in a specified order by using sorted() method.

 

Following is an example of iterating over Set elements using the for-in loop.

 

var names: Set = ["suresh", "rohini", "praveen", "sateesh"]

for name in names.sorted() {

print(name)

}

When we run above example we will get result like as shown below.

 

praveen

rohini

sateesh

suresh

This is how we can loop through or iterate over Set elements in swift programming language based on our requirements.

Swift Set Operations

In swift, we can perform some of the mathematical operations like Union, Intersection, Subtract, etc. on Sets based on our requirements. 

 

Suppose if we have two Sets, we easily perform mathematical operations like Union, Intersection and etc. based on our requirements.

Union Operator

In swift union operator is useful to create a new Set by combining the values of two sets.

 

Following is the example of using Union operation to combine two sets.

 

var names: Set = ["suresh", "rohini", "praveen", "sateesh"]

var names1: Set<String> = ["sudheer","madhav","tulasi"]

let unionSet = names.union(names1)

print(unionSet)

If you observe above example we are adding two sets elements into single using union operator.

 

When we run the above example we will get result like as shown below

 

[madhav, suresh, rohini, sateesh, sudheer, praveen, tulasi]

Intersect Operator

In swift Intersect operator is used combine multiple Sets and create new Set with the common elements of multiple sets.

 

Following is the example of using Intersect operation to combine two sets to get only common elements.

 

var names: Set = ["suresh", "rohini", "praveen", "sateesh"]

var names1: Set<String> = ["sudheer","rohini","suresh"]

let intersectSet = names.intersect(names1)

print(intersectSet)

If you observe above example we are combining two set elements to get common elements from both the sets using intersect operator.

 

When we run the above example we will get a result like as shown below

 

[rohini, suresh]

Subtract Operator

In swift subtract operator is used to remove the values which are same in both the Sets and return remaining elements in first Set.

 

Following is the example of using Subtract operation to remove common elements in the first set and return remaining elements in first set.

 

var names: Set = ["suresh", "rohini", "praveen", "sateesh"]

var names1: Set<String> = ["sudheer","rohini","suresh"]

let subtractSet = names.subtract(names1)

print(subtractSet)

If you observe above example we are combining two set elements by using subtract operator to remove the common elements in first Set and to return remaining elements in first Set.

 

When we run the above example we will get a result like as shown below

 

[praveen, sateesh]

Exclusive-Or Operator

In swift, exclusive-or operator is used to combine multiple sets and create a new set with unique values from all the sets.

 

Following is the example of using exclusive-or operation to combine multiple sets to return only unique values.

 

var names: Set = ["suresh", "rohini", "praveen", "sateesh"]

var names1: Set<String> = ["sudheer","rohini","suresh"]

let exclusiveSet = names.exclusiveOr(names1)

print(exclusiveSet)

If you observe above example we are combining two set elements by using exclusiveOr operator to get only unique elements from both the sets.

 

When we run the above example we will get a result like as shown below

 

[praveen, sateesh, sudheer]

This is how we can perform Set operations in a swift programming language based on our requirements.