Ankita Saini has Published 277 Articles

Swift Program to Remove Null from a Dictionary

Ankita Saini

Ankita Saini

Updated on 09-May-2023 11:43:32

Sometimes a dictionary contains null values so to remove those null values Swift provides an in-built function named the filter() method. In the filter() method, we pass a closure which returns a boolean value indicating whether the key-value pair should be included in the resultant dictionary or not. Or we ... Read More

Swift Program to Remove duplicate elements from Dictionary

Ankita Saini

Ankita Saini

Updated on 09-May-2023 11:41:07

In Swift dictionary, we cannot have duplicate keys but can have duplicate values hence in this article we will remove duplicate values from the dictionary. So for that, we use contains() function. This function checks if the dictionary contains duplicate values or not. If the dictionary contains duplicate values, then ... Read More

Swift Program to Remove an Element from a Dictionary

Ankita Saini

Ankita Saini

Updated on 09-May-2023 11:38:25

In Swift, we can remove an element from the specified dictionary using the removeValue() function. This function removes the specified key and its associated value from the given dictionary. Syntax Dict.removeValue(forKey: key) Here, the key represents the key which we wanted to remove from the dictionary along with its ... Read More

Swift Program to Remove all the elements from Dictionary

Ankita Saini

Ankita Saini

Updated on 09-May-2023 11:14:25

In a dictionary, to remove all the elements from the dictionary Swift provides an inbuilt function named as removeAll() function. It will remove all the key-value pairs from the specified dictionary. Syntax dict.removeAll() Here, dict is the dictionary. The removeAll() function does not take any parameter and removes all ... Read More

Swift Program to Print the keys and values of a Dictionary

Ankita Saini

Ankita Saini

Updated on 09-May-2023 11:12:44

A dictionary is an unordered collection in which data is stored in the form of key-value pairs. To print the keys and values of a dictionary Swift supports the following methods − Using for-in loop Using properties Method 1: Using for-in Loop To print keys and values of ... Read More

Swift Program to Print even length words

Ankita Saini

Ankita Saini

Updated on 09-May-2023 11:09:56

To print even-length words we calculate the length of each word using the count property. Then we check if the length of the word is even or not. If yes, then we will print the word. Otherwise not. Input Str = “Learn Swift language” Output language Here the ... Read More

Swift Program to Print a Dictionary

Ankita Saini

Ankita Saini

Updated on 09-May-2023 11:02:11

In Swift, a dictionary is used to create an unordered collection in which the data is stored in the form of key-value pairs. So to print a dictionary we will use the following methods − Using for-in loop Using description property Method 1: Using for-in Loop In Swift, ... Read More

Swift Program to Pass Dictionary as the function argument

Ankita Saini

Ankita Saini

Updated on 09-May-2023 10:59:51

A Swift dictionary is an unordered collection in which data is stored as key-value pairs. So to pass a dictionary as a function argument simply create a function with an argument of dictionary type and a dictionary, then pass it to the function at the time of function calling. Syntax ... Read More

Swift Program to merge two integer arrays without using library function

Ankita Saini

Ankita Saini

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

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 ... Read More

Swift Program to Merge two Dictionaries

Ankita Saini

Ankita Saini

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

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 ... Read More

Advertisements