Table of contents
1.
Introduction
2.
Creating a swift set
3.
Adding elements to a swift sets
4.
Removing Element from the Swift sets
5.
Swift set operation
6.
Frequently Asked Questions
6.1.
Are there sets in swift?
6.2.
What is the difference between a swift array and swift sets?
6.3.
What is hashable swift?
6.4.
What are swift sets?
6.5.
How do we declare a swift set?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Swift sets

Author yuvatimankar
0 upvote

Introduction

A Swift set is a container that can hold multiple values of a data type in an unordered list. An unordered list means you will not get the items in the same order in which you have entered them in the set.

A set is nothing but the collection of unique data which means a set cannot contain duplicate values. Swift sets make sure that an item appears only once an order of the item is not important. In swift sets, the value stored must be hashable, which means it must provide a hashValue property. As sets are unordered, HashValue is used to access the elements of the sets. 

Creating a swift set

We can create a set in Swift, as shown below.

var Marks: Set = [101, 102, 103, 105, 108]
print(“Marks: \(Marks)”)

 

Output:

Marks: [101, 102, 103, 105, 108]

 

Here, the set keyword specifies that Marks is a set. Since all the elements of the set are integers, Marks is a set of int types.

Adding elements to a swift sets

For adding elements to a swift set, the insert() method is used.

var StudentID:  Set = [24, 36, 41, 55, 60]
print(“Initial Set: \(studentID)”)

// Inserting element
numbers.insert(32)

print(“Updated Set: \ (numbers)”)

 

Output: 

Initial Set: [24, 36, 41, 55, 60] 
Updated Set: [ 24, 36, 41, 55, 60, 32]

Removing Element from the Swift sets

For removing a specified element from the set, the remove() method is used.

var languages: Set = ["Swift", "CPP", "Python"]
print("Initial Set: \(languages)")

// Remove CPP from a set
let removedValue = languages.remove("Java")
print("Set after remove(): \(languages)")

 

Output:

Initial Set: ["Python", "CPP", "Swift"]
Set after remove(): ["Python", "Swift"]

Swift set operation

Swift Set provides various built-in methods for mathematical set operations like union, subtraction, intersection, and symmetric difference.

  • Union of Two sets 

To perform the set union operation, the union() method is used.

// First set
let setA: Set = [1, 3, 5]
print("Set A: ", setA)

// Second set
let setB: Set = [0, 2, 4]
print("Set B: ", setB)

// Perform union operation
print("Union: ", setA.union(setB))

 

Output:

Set A:  [1, 5, 3]
Set B:  [0, 2, 4]
Union: [0, 5, 2, 4, 1, 3]

 

  • The intersection between two sets

To perform an intersection between two sets, the intersection() method is used.

// First set
let setA: Set = [1, 3, 5]
print("Set A: ",  setA)

// Second set
let setB: Set = [1, 2, 3]
print("Set B: ",  setB)

// Perform intersection operation
print("Intersection: ", setA.intersection(setB))

 

Output:

Set A:  [5, 3, 1]
Set B:  [1, 2, 3]
Intersection:  [3, 1]

 

  • Difference between the two sets

To perform the difference between two sets, the subtracting() method is used.

// First set
let setA: Set = [2, 3, 5]
print("Set A: ",  setA)

// Second set
let setB: Set = [1, 2, 6]
print("Set B: ",  setB)

// Perform subtraction operation
print("Subtraction: ", setA.subtracting(setB))

 

Output:

Set A:  [3, 5, 2]
Set B:  [1, 6, 2]
Subtraction:  [3, 5]

 

  • A subset of a set

To check if one set is a subset of another or not, the Subset() method is used.

// First set
let setA: Set = [1, 2, 3, 5, 4]
print("Set A: ",  setA)

// Second set
let setB: Set = [1, 2]
print("Set B: ",  setB)

// Check if setB is subset of setA or not
print("Subset: ", setB.isSubset(of: setA))

 

Output:

Set A:  [3, 1, 2, 5]
Set B:  [1, 2]
Subset:  true

 

  • Check if two sets are equal

We can check if two sets are equal by using the equality operator.

let setA: Set = [1, 3, 5]
let setB: Set = [3, 5, 1]

if setA == setB {
  print("Set A and Set B are equal")
}
else {
  print("Set A and Set B are different")
}

 

Output:

Set A and Set B are equal

Frequently Asked Questions

Are there sets in swift?

Swift provides three primary collection types, known as arrays, sets, and dictionaries, for storing collections of values.

What is the difference between a swift array and swift sets?

The main difference between the Swift array and Swift sets is that the Swift set cannot store the same value twice unlike the Swift array.

What is hashable swift?

Hashable swift means it must provide a hash value property. A hashValue is nothing but an integer that is the same for any two instances that compare equally.

What are swift sets?

Swift sets are used to store different values of the same types but they are unordered.

How do we declare a swift set?

We can declare a set that can store some values.

let someIntSet:Set = [1, 2, 3, 4, 5, 6, 7, 8, 9]  
print(someIntSet)  

Conclusion

In this article, we have extensively discussed swift sets and the various Swift set operation with the help of examples. 

After reading about swift closures, are you not feeling excited to read/explore more articles on the topic of Swift? Don’t worry; Coding Ninjas has covered you. To learn, see Swift MethodsSwift InitializationTimers in SwiftSwift Inheritance, and Swift Generics.

Refer to our guided path on Coding Ninjas Studio to upskill yourself in Data structure and algorithmsCompetitive Programming, JavascriptSystem Design, and many more if you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio But if you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Nevertheless, you may consider our paid courses to give your career an edge over others!

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass