Found 33676 Articles for Programming

Swift Program to Find Common Array Elements

Ankita Saini
Updated on 20-Dec-2022 10:56:51

2K+ Views

This article will teach us how to write a swift program to find common array elements. So here we use the following methods − Using the filter method Using sets But before moving to the methods, we first understand common elements with the help of an example. Suppose we have two arrays − Arr1 = [2, 4, 6, 89, 78] Arr2 = [56, 88, 32, 4, 99, 89] So the common elements between two arrays are [4, 89]. Because they are available in Arr1 and Arr2. Method 1: Using filter(_:) Method To find common elements between two arrays ... Read More

Swift Program to Count the elements of an Array

Ankita Saini
Updated on 20-Dec-2022 10:53:35

311 Views

In this article, we will learn how to write a swift program to count the elements of an array. To count the elements of an array swift provide a count property. This property will return the total number of elements present in the specified array. For example − Arr = [3, 5, 6, 4, 6] so the count will return 5. Syntax var count: Int{get} Or you can also write it as − arrayName.count Here count is the property of the array to count the total elements present inside the array. To use this property we ... Read More

Swift Program to Check if Two Arrays Are Equal or Not

Ankita Saini
Updated on 20-Dec-2022 10:50:47

4K+ Views

In this article, we will learn how to write a swift program to check whether two arrays are equal. To check if two arrays are equal to not either, we are going to use the following two methods − using == operator elementsEqual(_:) method. Both method and operator return true if both the given array has the same elements. Otherwise, they will return false. For example, arr1 = [2, 3, 4, 5] and arr2 = [2, 3, 4, 5] both arrays are equal so both the method and operator will return true. arr1 = [2, 3, 4, ... Read More

Swift Program to Check if an array is empty

Ankita Saini
Updated on 20-Dec-2022 10:38:52

4K+ Views

In this article, we will learn how to write a swift program to check if an array is empty. Here we use two methods: Using isEmpty property Using conditional statements Method 1: Using isEmpty Property To check if an array is empty Swift provides an isEmpty property. This property will return true if the given array is empty or return false if the given array is not empty. For example Arr = [3, 5, 6, 4, 6] so isEmpty property will return false. Syntax var isEmpty: Bool{get} Or you can also write it as − arrayName.isEmpty ... Read More

What are type guards in typescript?

Shubham Vora
Updated on 19-Dec-2022 16:20:34

3K+ Views

In TypeScript, the type guards are used to determine a variable's type, often inside a conditional or functional block. The type guards usually take the variable and return a Boolean value or the variable type. Type guards allow you to tell the TypeScript compiler to infer a given type for a variable in a specific context, guaranteeing that an argument's type is what you say it is. Like feature detection, type guards are frequently used to limit a type and let you identify the proper prototypes, methods, and attributes of a value. As a result, handling that value became simple ... Read More

Method Overriding in TypeScript?

Shubham Vora
Updated on 19-Dec-2022 16:17:06

15K+ Views

The method overriding means writing the new definition of the method in the inherited class by keeping the method name, parameters, and return type the same. In this tutorial, we will learn about the method overriding in TypeScript. Inheritance is one of the four pillars of object-oriented programming. The method overriding feature of the OOPs is helpful with the inheritance. For example, we can define the method in the parent class and redefine the method with the same name, parameter, and return type into the base class with a different code to execute, and that’s how method overriding comes into ... Read More

How to use getters and setters in TypeScript?

Shubham Vora
Updated on 19-Dec-2022 16:06:17

9K+ Views

In TypeScript, the getters and setters are two terms that can be used to get and set the value of the class members, respectively. However, users can access the public members of the class directly via the dot operator by taking the object of the particular class as a reference. To access the class's private members, we just need to use the getter method. In this tutorial, we will learn to use the getters and setters in TypeScript. Using the getters to access the private members of the class As we create a method to access the class's private ... Read More

How to sort a 2D array in TypeScript?

Shubham Vora
Updated on 19-Dec-2022 16:03:46

5K+ Views

In TypeScript, a 2D array is an array of arrays, where each inner array represents a row of the 2D array. You can access individual elements of the array using bracket notation, with the row and column indices separated by a comma. A single-dimensional array can store values of any data type, such as string, number, objects, etc. To sort a 2D array in TypeScript, you can use the sort() method along with a custom compare function. The sort() method takes the compare function as an optional argument, and the compare function should take two arguments, a and b, ... Read More

How to push an element to the last of an array in TypeScript?

Shubham Vora
Updated on 19-Dec-2022 16:02:09

3K+ Views

In TypeScript, an array is a data type that represents a collection of elements. Each element in the array has a specific index, or position, in the array, and you can access or modify the elements in the array using their index. In TypeScript, an array can contain elements of the same or different data types. To push an element to the end of an array in TypeScript, you can use the push() method. This method adds the element to the end of the array and returns the new length of the array. Syntax Users can follow the syntax below ... Read More

How to find the length of a string in TypeScript?

Shubham Vora
Updated on 19-Dec-2022 15:56:59

4K+ Views

The string can contain a sequence of repeated and non-repeated characters. The length of the string means the total number of characters the string contains in the sequence of characters, including special characters, alphabetic characters, etc. Here, we will learn different methods to calculate the length of the string in TypeScript. Using the length property of the string In TypeScript, the string is a library, or we can say it is a class. It contains some properties and methods, which we can call by taking the string class object as a reference. The length is also the property of the ... Read More

Advertisements