Found 517 Articles for Swift

Swift Program to Reverse a Set

Ankita Saini
Updated on 24-Apr-2023 09:40:02

142 Views

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

Swift Program to Remove Null from a Set

Ankita Saini
Updated on 24-Apr-2023 09:39:28

268 Views

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

Swift Program to Remove a Subset from a Set

Ankita Saini
Updated on 24-Apr-2023 09:38:12

187 Views

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

Swift Program to Recursively Linearly Search an Element in an Array

Ankita Saini
Updated on 24-Apr-2023 09:36:11

166 Views

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

Swift Program to read and print two-dimensional array

Ankita Saini
Updated on 24-Apr-2023 09:34:42

509 Views

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

Swift Program to Print a Set

Ankita Saini
Updated on 24-Apr-2023 08:59:33

306 Views

In Swift, a set is used to define a collection of unique elements, means a set doesn’t contains duplicate elements. In a set, the elements are not arranged in a particular order. To print a set we can use for-in loop, forEach() function, as well as the name of the set. Lets discuss all the methods one by one in detail along with examples. Method 1: Name of the Set To print a set we simply call the name of the set in the print() function. It will display set elements in the square bracket([]). Or we can say that ... Read More

Swift Program to Merge Two Sets

Ankita Saini
Updated on 21-Apr-2023 13:48:12

916 Views

Merging two sets means combining all the elements of a set into another set without any duplicates. In Swift, we can either use formUnion() function or for-in loop to merge the elements of two sets. Lets discuss both the methods in detail along with examples. Method 1: Using formUnion() Function To merge two sets we can use formUnion() function. The formUnion() function is used to insert the elements of the given sequence into the set. Here the sequence can be anything a set, array, etc. This function merge two sets without any duplicates. Syntax set1.formUnion(set2) Where set1 and set2 ... Read More

Swift Program to Iterate Over an Set

Ankita Saini
Updated on 21-Apr-2023 13:45:20

460 Views

In Swift, a set is used to define a unordered collection of unique elements. To iterate over an set swift provide inbuilt forEach() function as well as for-in loop. Lets discuss both the methods in detail along with examples. Method 1: Using for-in Loop To iterate over an set we can use for-in loop. The for-in loop iterate through each element of the set and display them on the screen one by one. Syntax for x in newSet{ print(x) } Where newset is the name of the set, and x variable store the current element from ... Read More

Swift Program to Initializing a Set

Ankita Saini
Updated on 21-Apr-2023 13:47:07

68 Views

In Swift, a set is used to define a collection of unique elements, means a set doesn’t contains duplicate elements. In a set, the elements are not arranged in a particular order. Initialising a set means to store some information in the set. It can be done at the time of declaration or after declaration. We can initialise a set in various way like initialising a set without explicitly passing data types, initialising a set with explicitly passing data types, and using inbuilt insert() function. Now lets discuss all the method in detail along with examples. Method 1 In this ... Read More

Swift Program to Implement Linear Search Algorithm

Ankita Saini
Updated on 21-Apr-2023 11:38:05

395 Views

In Swift, the Linear search algorithm is the easiest search algorithm. In a linear search algorithm, searching starts from index 0 and checks all the elements of the given array/sequence one by one until the desired element is found. If the desired element is found, then return the index of that element. If the desired element is not found, then return null. For example − We have an element M = 13, now we search M in the following array using a linear search algorithm − The linear search algorithm starts from index 0 and compares M=13 with ... Read More

Advertisements