Found 517 Articles for Swift

Swift Program to get the last given number of items from the array

Ankita Saini
Updated on 27-Jan-2023 12:30:54

120 Views

In this article, we will learn how to write a swift program to get the last given number of items from the array. An array is used to store elements of same data type in an order. So, here we are using the following methods to get the last given number of items from the array − Using user defined function Using suffix(_:) function Using suffix(from:) function Method 1: Using a user-defined function To get the last given number of items from the array we can create a function. This function will return an array containing the ... Read More

Swift Program to get the first given number of items from the array

Ankita Saini
Updated on 27-Jan-2023 12:29:23

137 Views

In this article, we will learn how to write a swift program to get the first given number of items from the array. An array stores elements of the same data type in order. Therefore, here we use the following methods to get the first given number of items from the array − Using subscript with range Using prefix(_:) function Using prefix(upTo:) function Method 1: Using subscript with range To get the first given number of items from the array we can use subscript with different range operators like closed range operator, closed range operator with one-sided ranges, ... Read More

Swift Program to fill an array with a specific element

Ankita Saini
Updated on 27-Jan-2023 12:28:31

668 Views

In this article, we will learn how to write a swift program to fill an array with a specific element. An array is used to store elements of same data type in an order whereas a set is used to store distinct elements of same data type without any definite order. In an array, every element has an index. The array index is start from 0 and goes up to N-1. Here N represent the total number of array elements. We can fill an array with a specified element using the following methods − Using user defined function Using ... Read More

Swift Program to remove all 'nil' elements from the array

Ankita Saini
Updated on 17-Jan-2023 17:36:39

825 Views

In this article, we will learn how to write a swift program to remove all ‘nil’ elements from the array. Till now we all know that an array can store elements of a single data type. But Swift array can also hold elements of multiple data types. To store multiple data types in the array we use [Any], where [Any] specifies that the address can hold elements of any data types. Similarly an array also contain nil value. Here we use the following methods to remove nil elements from the array. Using compactMap() function Using filter() function ... Read More

Swift Program to find the prime numbers from the array

Ankita Saini
Updated on 17-Jan-2023 17:40:27

2K+ Views

In this article, we will learn how to write a swift program to find the prime numbers from the array. Prime numbers are those numbers that are only divisible by 1 and itself. Or we can say prime number have only two factors that are 1 and the number itself. For example, 1, 2, 7, 3, 11, 31, etc. So to find the prime numbers from the given array, we individually check each element of the given array is prime or not. Algorithm Step 1 − Create a function. Step 2 − Check if the number is greater than ... Read More

Swift Program to find the EVEN numbers from the array

Ankita Saini
Updated on 17-Jan-2023 17:20:51

1K+ Views

In this article, we will learn how to write a swift program to find even numbers from the array. Even number are those number which are completely divisible by 2. For example, 2, 6, 50, 20, etc. Here we use the following methods to find the even numbers from the array. Using for-in loop Using filter() function Method 1: Using for-in loop To find the even numbers from the given array we use for-in loop. Using for-in loop we iterate through each element of the given array and check if the element is even number or not. ... Read More

Swift Program to fetch elements from an array based on an index

Ankita Saini
Updated on 17-Jan-2023 17:15:02

295 Views

In this article, we will learn how to write a swift program to fetch elements from an array based on an index. Using array syntax An array is used to store elements of same data type in an order whereas a set is used to store distinct elements of same data type without any definite order. In an array, every element has an index. The array index is start from 0 and goes up to N-1. Here N represent the total number of array elements. To retrieve elements from the array according to their index we can use subscript syntax. ... Read More

Swift Program to Convert Set of String to Array of String

Ankita Saini
Updated on 17-Jan-2023 17:13:20

2K+ Views

In this article, we will learn how to write a swift program to convert set of string to array of string. An array is used to store elements of same data type in an order whereas a set is used to store distinct elements of same data type without any definite order. Now to convert a set of string to an array of string we use the following two methods − Using Array() initializer Using map() function Method 1: Using Array() Initializer To convert a set of string to an array of string we can use Array() initializer. ... Read More

Swift Program to append an element into an array

Ankita Saini
Updated on 17-Jan-2023 17:09:25

1K+ Views

In this article, we will learn how to write a swift program to append an element into an array. An array is used to store elements of same data type in an order whereas a set is used to store distinct elements of same data type without any definite order. Here we use the following methods to append an element into an array − Using append(_:) function Using operator Method 1: Using append(_:) function To append an element in the array we uses append( function. The append(_:) function adds a new element at the end of the array. ... Read More

Swift Program to Subtract Two Matrix Using Multi-dimensional Arrays

Ankita Saini
Updated on 09-Jan-2023 15:22:58

262 Views

In this article, we will learn how to write a swift program to subtract two matrices using multi-dimensional arrays. A matrix is a mathematical structure in which the elements are placed in rows and columns format. For example, the first element is present at a00 location, the second at a01, and so on. So to subtract two matrices we are going to use the - operator to subtract the elements of two matrices like a00 - b00 and then store the sum into a new matrix. For example − Matrix 1 − $\mathrm{\begin{bmatrix}1 & 8 & 4 ewline8 & 8 ... Read More

Advertisements