Found 33676 Articles for Programming

Golang program to merge two slices

Akhil Sharma
Updated on 13-Feb-2023 15:57:29

6K+ Views

In Golang, a slice is a sequence of elements just like an array. An array is a fixed sequence of elements whereas slice is a dynamic array which means its value is not fixed and can be changed. Slices are more efficient and faster than arrays moreover they are passed by reference instead by value. Syntax func append(slice, element_1, element_2…, element_N) []T The append function is used to add values to an array slice. It takes number of arguments. The first argument is the array to which we wish to add the values followed by the values to add. ... Read More

Swift Program to Show Overloading of Methods in Class

Ankita Saini
Updated on 08-Feb-2023 20:07:01

253 Views

Method overloading is a technique that allows us to create multiple methods of same name within a single class but with different parameter types or different number of parameters. The specific method that is called is determined by the types or number of arguments passed to it at the time of the method call. For example − class Book { // Methods func Author(){} func Author(x: Int)->Int{} func Author(x: String)->String{} func Author(a: Int, b: String)->Int{} } Here, the class Book has four overloaded methods. ... Read More

Swift Program to Iterate Over an Array

Ankita Saini
Updated on 08-Feb-2023 20:09:08

4K+ Views

An array is used to store elements of same data type in an order. In this article, we will learn how to write a swift program to iterate over an array. Here we use the following methods to iterate through each element of the array − Using for-in loop Using forEach() function Using while loop Using enumerated() function Method 1: Using for-in loop To iterate over an array we can use for-in loop. Syntax for value in Sequence { // statement } Here, the value parameter contains an element of the array during the ... Read More

Swift Program to insert multiple elements into the array from the specified index

Ankita Saini
Updated on 08-Feb-2023 20:14:53

1K+ Views

An array is used to store elements of same data type. In this article, we will learn how to write a swift program to insert multiple elements into the array from the specified index. Here we use the following methods to insert multiple elements into the array from the specified index − Using Index Using insert(contentsOf:at:) function Method 1: Using Index Here we use square brackets[] along with rang operators to insert multiple elements into the array at the specified index. Algorithm Step 1 − Create an array of integer. Step 2 − Create another array ... Read More

Swift Program to get array elements after N elements

Ankita Saini
Updated on 08-Feb-2023 20:15:22

470 Views

An array is used to store elements of same data type in an order. So now we extract the array elements after N elements. For example − Array : [2, 4, 5, 6, 7, 9] N = 3 ResultArray = [6, 7, 9] In this article, we will learn how to write a swift program to get array elements after N elements. Algorithm Step 1 − Create function. Step 2 − Create a new array using Array() initializer that containing elements after N elements. Step 3 − Return the resultant array. Step 4 − Create an array and a ... Read More

Swift Program to find the index of the first occurrence of the specified item in the array

Ankita Saini
Updated on 08-Feb-2023 20:16:08

845 Views

An array is used to store elements of the same data type in order. Here we will learn how to write a swift program to find the index of the first occurrence of the specified item in the array. To achieve that we are going to use the following methods to find the index of the first occurrence of the array element − Using user-defined function Using firstIndex() function Method 1: Using user-defined function To find the index of the first occurrence of the specified element in the array we create a function which iterate through each elements ... Read More

Swift Program to find the given number is strong or not

Ankita Saini
Updated on 08-Feb-2023 20:16:57

261 Views

A strong number is a special number in which the sum of the factorial of its digit is equal to the number itself. For example − Number = 345 345! = 3! + 4! + 5! = 6 + 24 + 120 = 150 Here 345 is not a strong number because the sum of the factorial of its digit is not equal to the number itself. Number = 145 145! = 1! + 4! + 5! = 1 + 24 + 120 = 145 Here 145 is a strong number because the sum of the factorial of its ... Read More

Swift Program to find the 1's complement of the given number

Ankita Saini
Updated on 08-Feb-2023 20:17:46

775 Views

1’s complement of a binary number is the invert of the given number. For example, we have a number = 10101011, so the one’s complement is 01010100. Here we using the following two methods to find one’s complement − Replacing 1s with 0s and 0s with 1s Using XOR In this article, we will learn how to write a swift program to find the 1’s complement of the given number. Method 1: Replacing 1s with 0s and 0s with 1s It is the smallest method to find ons complement of the given number. Here we simply convert the given number ... Read More

Swift Program to Convert Array to Set

Ankita Saini
Updated on 08-Feb-2023 20:19:03

9K+ Views

An array is used to store elements of the same data type in an order whereas a set is used to store distinct elements of the same data type without any definite order. To convert Array into set we use Set() initialiser. The resultant set contains the same elements as the original array but with no order and no duplicate elements. In this article, we will learn how to convert Array to Set using Swift programming language. Algorithm Step 1 − Create an array of integer type. Step 2 − Print the array. Step 3 − Convert array into ... Read More

Swift Program to check whether a given string is Heterogram or not

Ankita Saini
Updated on 08-Feb-2023 21:13:54

266 Views

Heterogram string is a string in which no alphabet occurs more than once. For example, “Sky with cloud” is a heterogram because in the string each alphabet occurred only once. Whereas “colorful balloons” is not a Heterogram because in the string some alphabet like o, u, l, etc appears more than once. Here we will execute different examples that will check whether the given string is heterogram or not using swift programming language. Algorithm Step 1 − Create a function. Step 2 − Remove the white space from the string. Step 3 − Create a separate array to ... Read More

Advertisements