Ankita Saini has Published 319 Articles

Swift Program to Search an Element in a Dictionary

Ankita Saini

Ankita Saini

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

880 Views

In a Swift dictionary, we can search for an element that is either a key or value with the help of contains() function. The contains() function will return a boolean value which represents whether the specified element(key or value) is found or not. Syntax dict.contains{$0.key == “KeyName”} Here, dict ... Read More

Swift Program to Replace Elements in a Dictionary

Ankita Saini

Ankita Saini

Updated on 09-May-2023 11:49:30

129 Views

In Swift, a dictionary is an unordered collection of key-value pairs. So, to replace the value of a key Swift provides bracket notation or subscript notations. Using this notation we can also add new key-value pair in the dictionary if the specified pair does not exist. Syntax dict[keyName] = “Value” ... Read More

Swift Program to Remove Null from a Dictionary

Ankita Saini

Ankita Saini

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

394 Views

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

460 Views

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

524 Views

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

122 Views

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

581 Views

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

61 Views

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

518 Views

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

668 Views

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

Previous 1 ... 4 5 6 7 8 ... 32 Next
Advertisements