Pratima S

The properties of value types cannot be modified within its instance methods by default. In order to modify the properties of a value type, you have to use the mutating keyword in the instance method.

With this keyword, your method can then have the ability to mutate the values of the properties and write it back to the original structure when the method implementation ends.

If you create a constant instance and then try to use mutating method

Swift refuses to build your code again — we’re trying to call a mutating function on a constant struct, which isn’t allowed.



Arrays, sets, and dictionaries are called collections, because they collect values together in one place.

If you want to create an empty collection just write its type followed by opening and closing parentheses.

var teams = [String: String]()

We can then add entries later on, like this:

teams["Paul"] = "Red"

create an empty array to store integers like this:

var results = [Int]()

The exception is creating an empty set, which is done differently:

var words = Set<String>()
var numbers = Set<Int>()

If you wanted, you could create arrays and dictionaries with similar syntax:

var scores = Dictionary<String, Int>()
var results = Array<Int>()



Whenever you read a value from a dictionary, you might get a value back or you might get back nil — there might be no value for that key. Having no value can cause problems in your code, not least because you need to add extra functionality to handle missing values safely, and that’s where dictionary default values come in: they let you provide a backup value to use for when the key you ask for doesn’t exist.

For example, here’s a dictionary that stores the exam results for a student:

let results = [
"english": 100,
"french": 85,
"geography": 75

As you can see, they sat three exams and scored 100%, 85%, and 75% for English, French, and Geography. If we wanted to read their history score, how we do it

let historyResult = results["history", default: 0]



Dictionaries and arrays are both ways of storing lots of data in one variable, but they store them in different ways:

dictionaries let us choose a “key” that identifies the item we want to add, whereas arrays just add each item sequentially.

So, rather than trying to remember that array index 7 means a user’s country, we could just write user[“country”] — it’s much more convenient.

Like sets, dictionaries don’t store our items in a specific order, so they optimize the way they store items for fast retrieval. So, when we say user[“country”] it will send back the item at that key (or nil) instantly, even if we have a dictionary with 100,000 items inside.



  • Read code and write lots of code
  • Practice coding
  • Break the problem into a subproblem
  • learn new concept every day
  • Coding is all about practice — the more you practice code, the better you get at it.
  • Do a formal course on Data Structures and Algorithms. A proper course teaches you the intricacies of the data structures and algorithms
  • Solve the top 100 most solved questions on SPOJ
  • Once you’ve solved around 100 problems, you will feel much confident. Then, you can move on to learning more advanced concepts. You will begin to have fun while coding.



  • Both sets and arrays are collections of data, meaning that they hold multiple values inside a single variable
  • sets are unordered and cannot contain duplicates, whereas arrays retain their order and can contain duplicates.

because sets don’t need to store your objects in the order you add them, they can instead store them in a seemingly random order that optimizes them for fast retrieval. So, when you say “does this set contain item X,” you’ll get an answer in a split second no matter how big the set is.

In comparison, arrays must store their items in the order you give them, so to check whether item X exists in an array containing 10,000 items Swift needs to start at the first item and check every single item until it’s found