Swift Sets Collection Type

  Total Views : 251
  Comments : 0
  Keywords : Swift sets are used to store values of same type of elements in unordered list, Swift sets are strongly typed because it allow only one data type of values to store

Here we will learn sets in swift with examples and how to use swift sets to store unique elements in unordered manner, create sets, add or remove elements in sets, access set elements, count or check set is empty or not, iteration over a set elements and set operations (union, intersect, subtract, exclusiveor) with examples.

Swift Sets

In swift sets are used to store same type of distinct values in 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 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

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 elemetns in swift programming language with examples.

Accessing Set Elements

In swift we can access Set elements by using some of 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 first element in Set using 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 above program in swift playground that will return 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 first element of Set in 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 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 number of elements exists in sets in 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 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 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 an elements in sets based on our requirements in swift programming language.

 

Following is the example of iterating over an elements in set by using 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 doesn’t have any defined order. We can iterate over Set of elements in a specified order by using sorted() method.

 

Following is example of iterating over Set elements using 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 mathematical operations like Union, Intersection, Subtract, etc. on Sets based on our requirements. 

 

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

Union Operator

In swift union operator is used to create 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 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 above example we will get 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 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 above example we will get result like as shown below

 

[praveen, sateesh]

Exclusive-Or Operator

In swift exclusive-or operator is used to combine multiple sets and create 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 above example we will get result like as shown below

 

[praveen, sateesh, sudheer]

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

 

PREVIOUS

Swift Arrays
 
 

 Comments (0)

Be the first to give your valuable feedback

Leave a comment

  • Default Tutlane User