Swift provide an in built function named as shuffle() to shuffle the elements of an array. This function takes a sequence or array and then shuffle the places of the elements present in the given array. Syntax func shuffle() Where this function does not take any parameter. Also it does not return any value instead it shuffle the places of the element in the given collection or array. Example In the following swift example, we will create and initialise an array of string type, then we shuffle the elements of the array using shuffle() function and display output. import ... Read More
In Swift, super keyword in used to access or call the methods, properties, and initialisers of the superclass from the base class. Or we can say that super keyword add more functionality to the superclass by adding its own unique behaviour. You can use super keyword with class properties, methods, and initialisers. Example In the following swift example, we will create a superclass or base class named as Books with bookName property, init() initialiser and display() method. Then we create a subclass named as Author, it is inherits from Author class along with override initi() initialiser and display() method. In ... Read More
In Swift, static keyword is used to define or create type-level properties and method that only belongs to the class itself rather than to its instances. Or we can say that we can directly access the static properties and methods of a class with help of class itself instead of creating the instance of the class. Algorithm Step 1 − Create a class. Step 2 − Inside the class define static property using static keyword and a instance property. Step 3 − Also define a static method using and static keyword and a instance method. Step 4 − Outside ... Read More
In Swift, to search elements in a set we can use either contains(_:) function or linear search. Both the methods efficiently search an element in a set. Lets discuss both the methods in detail along with examples. Method 1: Using contains(_:) Function Swift provide an inbuilt function named as contains() function to search an element in a set. This function return true if the specified element is present in the given set. This function will return false if the specified element is present in the given set. Syntax sets.contains(myElement) Where sets is a finite set and myElement represent the ... Read More
In Swift, we create a user defined function to rotate the elements of an array. In this function, first we find the total number of rotation and then rotate the array by the specified number of rotation. For example − Array = [3, 5, 2, 1, 7, 9] Rotation = 2 Resultant Array = [2, 1, 7, 9, 3, 5] Algorithm Step 1 − Create a function that rotate the elements of the array in the specified number of rotation. Step 2 − Inside the function first we find the required number of rotations. Step 3 − Then ... Read More
In swift, a set is a unordered collection that only contain unique values. As we known that a set is an unordered collection so their is no direct way like an array to reverse the elements of the set. However you can reverse a set by converting it into an array and then apply reversed() method or any user defined function. And then convert back the result into set. Let discuss this method with the help of examples. Example 1 In the following example, we will have a set of string type. Now we convert it into array to ... Read More
In swift, a set is used to create an unordered collection of unique elements. In Swift, to store a nil value in the set you need to define the set as a optional type because nil is a optional value. So to remove null for a set we can use inbuilt functions like remove() and filter(). Lets discuss both the methods in detail along with examples. Method 1: Using remove(_:) Function The remove(_:) function is used to remove or delete a specified element from the given set. Syntax func remove(ele) Where ele is the element which we want to ... Read More
In Swift, a set is used to create an unordered collection of unique elements. To remove a subset from a set Swift provide two insult functions named as subtract() and subtracting(). Lets discuss both the methods in detail along with examples. Method 1: Using subtract(_:) Function The subtract(_:) function is used to remove the given subset from the specified set. Syntax func subtract(_subset:mSet) Where mSet is the subset which we want to remove from the given set and it must be a finite set. This function return a set after removing the common elements of set and subset. Example ... Read More
In Swift, linear search is the most easiest search algorithm. It is also known as sequential search because it check all the elements of the given array or sequence one by one. If the target element is found, then return the index of that array otherwise return element not found. So lets check if the given array contains the target element or not using recursive linear search. Algorithm Step 1 − Create a function to check if the given element is present in the specified array or not using recursive linear search. Step 2 − Inside the function, we ... Read More
Just like other programming languages Swift also supported two- dimensional array. A two dimensional array is known as an array of array that store same type of data in a tabular format. Or we can say 2-D array is an array which represent homogeneous data in rows and columns. Syntax var twoDArray:[[Int]] = [[2, 3], [3, 4]] To read two-dimensional array from the user Swift provide an inbuilt function named as readLine(). The readLine() function read a string of characters from the input. If the EOF has already been reached when this function is called, then it will ... Read More