Lukej2680 Tech Blog

Checkpoint 2

This post covers checkpoint 2 from 100 Days of Swift - Introduction to Swift module

Description
This time the challenge is to create an array of strings, then write some code that prints the number of items in the array and also the number of unique items in the array.

Code

var stringArray = Array<String>()

stringArray.append("Test1")
stringArray.append("Test2")
stringArray.append("Test3")
stringArray.append("Test1")

print(stringArray.count)

print(Set(stringArray).count)

Explanation
Here I create a new empty String array as a var since we will be modifying it throught the code.
Next I add 3 unique test elements to the array, with a 4th duplicate element.
From here I print the count of the array, then printing the count of the array after it has been cast to a Set object. This will remove the duplicate array entry as sets only contain unique values.

This project was taken from the HackingWithSwift website. However all code is original work by site owner.