We can use
.subtracting() as a set operation. By removing overlapping data from one set and adding it to another, subtracting method generates a new set of elements in Swift.
We can use the following code to use
.subtracting() in our program:
var SetSubtracting = SetA.subtracting(SetB)
Any values shared by
SetB will be removed, leaving
SetSubtracting with values only found in
SetA and not in
Consider the following two sets:
var Places: Set = ["Eiffel Tower", "Great Wall of China", "Kremlin", "Pyramid of Giza", "Taj Mahal", "Statue of Liberty"]; var visitedPlaces: Set = ["Taj Mahal", "Great Wall of China", "Kremlin"];
Based on the two sets above, we can use
.subtracting() to create a set of places that aren’t visited:
var noVisitedPlaces = Places.subtracting(visitedPlaces);
If we were to output the value of
noVisitedPlaces, the results would look something like this:
["Pyramid of Giza", "Eiffel Tower", "Statue of Liberty"]
Here are some useful tools to help you along your journey!
Setting up an IDE (Integrated Development Environment) can be difficult for beginners. The Online Compiler will enable you to run your code inside your browser without the need to install an IDE. If you need a more detailed explanation of a specific topic, the best place to find answers is in the Official Documentation.