Found 33676 Articles for Programming

Swift Program to merge two integer arrays without using library function

Ankita Saini
Updated on 09-May-2023 10:56:28

281 Views

In Swift, we can merge two or more integer arrays without using the library function. So Swift provides an addition assignment(+=) operator to merge two integer arrays. Using this operator we will merge two arrays and assign the result into a new array. Syntax newArray += array Here, newArray is the resultant array and array represents the array which we want to merge. Algorithm Step 1 − Create a function that takes two arrays as an argument and returns a merged array. Step 2 − Inside the function create an empty array to store the resultant array. Step ... Read More

Swift Program to Merge two Dictionaries

Ankita Saini
Updated on 09-May-2023 10:54:25

2K+ Views

To merge the content of two dictionaries Swift provides a pre-defined function named merging(_:uniqueKeysWith:). This function creates a new dictionary by merging the elements of two dictionaries. It also uses a combining closure to check the values for duplicate keys. Syntax func merging(otherDict){comClosure} This function takes two parameters and they are − OtherDict − It is a dictionary to merge. comClosure − It is a closure which takes current and new values for the duplicate keys and then returns the desired value for the output dictionary. It is an optional parameter that means if you don’t want ... Read More

Swift Program to Iterate through each character of the string

Ankita Saini
Updated on 09-May-2023 10:52:12

4K+ Views

In Swift, we can iterate through each character of the string very easily. For the iteration, Swift provides the following methods − Using for-in loop Using forEach() function Using enumerated() function Method 1: Using for-in loop We can use a for-in loop for the iteration. It iterates through each character of the string and then performs the expressions given inside the loop body or can display them on the output screen. Syntax for x in mstr{ // body } Here mstr is the string and x stores the current character from the string in ... Read More

Swift Program to Insert a string into another string

Ankita Saini
Updated on 09-May-2023 10:41:17

883 Views

Swift provides a function named insert() to insert a string into another string. The insert(at:) function adds a new character or string in the current string at the specified position. Input String = “Program to learn” New String = “Swift” Output String = Swift Program to learn” Here we insert a new string in the input String at index 1. So we get “Swift Program to learn” in the output. Syntax func insert(newVal, at: idx) Where newVal represents a new string which we want to insert into the given string, and idx is the valid index to ... Read More

__future__ Module in Python

Sarika Singh
Updated on 13-Jul-2025 00:03:01

396 Views

What is the __future__ Module? The __future__ is a built-in Python module that is used to import features from newer versions of Python into older versions. This helps you to write code that will work the same way in both old and new versions of Python. It is mainly used when you are migrating code from Python 2 to Python 3, or when you want to try out new features before they become standard (default) in future releases. For example, in older Python versions, dividing two integers would return an integer. But if you import the division feature like this: ... Read More

Swift Program to Get key from Dictionary using the value

Ankita Saini
Updated on 09-May-2023 10:35:05

3K+ Views

A dictionary is a collection of key-value pairs. Which means keys and values are interrelated with each other. So using a value we can easily get single or multiple keys from the given dictionary. Algorithm Step 1 − Create a dictionary with key-value pairs. Step 2 − Then create a function which will return the key from the dictionary using the value. Step 3 − Inside the function we run a for-in loop which iterates over the key-value pairs of the given dictionary. Step 4 − Now check if the current value is equal to the specified value. If ... Read More

Swift Program to Find the Frequency of Characters in a String

Ankita Saini
Updated on 09-May-2023 10:31:24

1K+ Views

In Swift, the frequency of all the characters in a string means how much time a character repeats in the given string. For example, “Swift tutorial”, here “t” repeats 3 times in the given string, “i” repeat 2 times in the given string. So in this article, we are going to find the frequency of all the characters in the string. Algorithm Step 1 − Create a variable to store a string. Step 2 − Create a dictionary to store the character and its count. Step 3 − Run a for loop to iterate over each character in the ... Read More

Swift Program to delete suffix substring from the given string

Ankita Saini
Updated on 09-May-2023 10:26:37

2K+ Views

To delete the suffix substring from the given string first, we check if the given substring is present in the specified string or not using the inbuilt hasSuffix() function. Then find the index of the suffix substring using the inbuilt index() function and finally remove the suffix substring. Input String = “Siya love cooking” Substring = “cooking” Output “Siya love” Here, the specified substring is found in the given string, so in the resultant string, we removed the substring from the end of the input string. Algorithm Step 1 − Create a string. Step 2 − Create ... Read More

Swift Program to delete prefix substring from the given string

Ankita Saini
Updated on 09-May-2023 10:24:21

1K+ Views

To delete the prefix substring from the given string first, we check if the given substring is present in the specified string or not using the inbuilt hasPrefix() function. Then find the index of the prefix substring using the inbuilt index() function and finally remove the prefix substring. Input String = “Today is cloudy day” SubString = “Today” Output “is cloudy day” Here, the specified substring is found in the given string, so in the resultant string, we removed the substring from the start of the input string. Algorithm Step 1 − Create a string. Step 2 ... Read More

Swift Program to Create an Empty Dictionary

Ankita Saini
Updated on 09-May-2023 10:21:44

799 Views

A dictionary is an unordered collection in which data is stored in the form of key-value pairs, where keys are the unique identifiers of any data type like string, integer, etc., that are connected with each value. In Swift, we are allowed to create an empty dictionary. An empty dictionary is a dictionary which does not contain any element or we can say that its size is zero. Syntax let myDicti = [Int:Int]() Or let myDicti:[Int:Int]= [:] Or let myDicti:[String:Int]= [:] So to create an empty dictionary you can use any of the given syntaxes. Here it ... Read More

Advertisements