Linear Search on a Java Array using RecursivelyLinear search is an algorithm that we use to check if an element is present in an array or not. It checks all the elements of the array one by one until it finds the element or reaches the end of the array. Recursion is a way where a function calls itself to solve a certain problem until it reaches a base case. In this article, we will understand how to recursively linearly search an element in an array. Let's see an example: Input: int [] arr = {1, 2, 3, 4, 5}; ... Read More
Jackson is a Java-based library, and it can be useful for converting Java objects to JSON and JSON to Java objects. A Jackson API is faster than other API, needs less memory, and is good for large objects. We can convert a JSON array to a list using the ObjectMapper class. It has a useful method, readValue(), which takes a JSON string and converts it to the object class specified in the second argument. Before proceeding further, first of all, we need to import the Jackson library into our project. To do this, you can add the following dependency to ... Read More
What is a Sparse Matrix? A matrix is said to be a sparse matrix if most of the elements of that matrix are 0. It means that the number of non-zero elements has a lesser count than the number of zero elements.Let's say we have a matrix of size 3*3, which means it has 9 elements. If 5 or more of those elements are 0, then we can say that the matrix is sparse. A = [ 0 2 0 ] [ 0 5 4 ] [ 0 0 0 ] ... Read More
In Java, a list is a collection that we use for storing elements. In many cases, we need to get the length of a list of lists. In this article, we will learn how to get the length of a list of lists in Java. List provides a method size() to get the count of elements present in the current list. To get the size of each list, we can iterate through each item in the list and add its size to get the count of all elements present in the list of lists. Ways to Get the Length of ... Read More
A List is a Collection in Java that is used for storing elements in sequence. In this article, we will be learning how to create a list with values in Java. Ways to Create a List with Values in Java There are various ways to create a list with values in Java. Below are the different approaches: Using Arrays.asList() method Using Stream.of() method Using List.of() method Using Arrays.asList() method The Arrays.asList() method returns the elements of the current array in the form of a List. This method ... Read More
In Python, duplicates of an existing file are created as backups to manipulate data and to preserve original version. To create a duplicate file of an existing file using Python we can use the shutil module or pathlib module, which we will discuss in detail with examples. Here are the various approaches for this: Creating Duplicate File of an Existing File Using shutil Module Creating Duplicate File of an Existing File Using open() Function Creating Duplicate File of an Existing File Using pathlib Creating Duplicate File ... Read More
Temporary files are created to store data temporarily during the execution of a program or while working with large amount of data. To create a temporary file we can use the tempfile module, as it creates unique names and stores in platform-dependent default location . Specifically the tempfile module is used as the files created using this module are deleted as soon as they are closed. Creating a Temporary File A temporary file is created using the TemporaryFile() function of the tempfile module. This by default opens the file in 'w+b' mode, which allows to both read and write the ... Read More
Catalan numbers are defined as a sequence of natural numbers that can be used to find the number of possibilities of various combinations. The below formula is used to calculate catalan number using the binomial coefficient ( denoted as (nk) and represents the number of ways to choose k items from n )- For example, if the input parameter n is given 6, the output would be 142, that is calculated using the above formula in the following way: C(6)=C(0)C(5) + C(1)C(4) + C(2)C(3) + C(3)C(2) + C(4)C(1) + C(5)C(0) Following are the two main methods used to calculate the ... Read More
In this article, we will discuss different methods used to right rotate a list from the given rotation number. A list has comma-separated values (items) of same type or different type between square brackets. To right rotate a list by n, Python offers many ways such as using slicing, rotate() method of the deque object and others. Let's discuss these ways in detail with examples - myList = [5, 20, 34, 67, 89, 94, 98, 110] The following is the output if we assign n as 4 − 89, 94, 98, 110, 5, 20, 34, 67 Right ... Read More
A Linked list is a linear type of data structure that is used to store a group of elements. It is a part of the Java Collections Framework. A LinkedList is made up of nodes, where each node contains a data field and a reference to the next node in the sequence. In this article, we will learn how to find an element in a LinkedList in Java. Ways to Find an Element in a LinkedList There are various ways to find an element in a LinkedList in Java. They are - Using the contains() ... Read More