Found 7442 Articles for Java

How to Prevent the Addition of Duplicate Elements to the Java ArrayList?

Adeeba Khan
Updated on 25-Aug-2023 17:19:53

3K+ Views

Widely used Java ArrayLists are data structures that offer dynamic arrays, enabling simple element manipulation. It is sometimes necessary to stop duplicate elements from being added to an ArrayList, though. Duplicate parts can cause your programme to behave unexpectedly, produce erroneous results, and use wasteful algorithms. This article will examine two methods to avoid adding duplicate components to a Java ArrayList, arming you with the information and resources you need to maintain data integrity and write cleaner code. Multiple disadvantages may result from adding duplicate elements to an array list. First, it might result in redundant data, which would use ... Read More

How to Print all Mappings of the LinkedHashMap in Java?

Adeeba Khan
Updated on 25-Aug-2023 17:29:29

585 Views

In Java, a LinkedHashMap is a popular data structure that combines the advantages of a doubly linked list with a hash map. The elements are retrieved in the same order as they were added since it preserves the sequence of insertion. When we need to iterate over the key-value pairs in a specified sequence, LinkedHashMaps are especially helpful. There are several methods accessible in situations where we want to print every mapping contained in a LinkedHashMap. In this article, we will examine two different methods for efficiently printing all LinkedHashMap's mappings in Java, each having its own benefits and sample ... Read More

Java Program to Minimize Characters to be changed to make the Left and Right Rotation of a String Same

Shubham Vora
Updated on 25-Aug-2023 15:24:18

133 Views

In this problem, we need to change the minimum characters in the given string to make its left and right rotations the same. In the string, we can observe that we can only make the left and right rotations of the string same if the string has an odd length and all characters are same, or the string has an even length and characters at even and odd indexes the same. For example, abab – Left and right rotation of the string is baba and baba. aaa – Left and right rotation of the stirng is aaa and aaa. ... Read More

Java Program to Find Maximum number of 0s placed consecutively at the start and end in any rotation of a Binary String

Shubham Vora
Updated on 25-Aug-2023 15:22:27

216 Views

In this problem, we will write Java code to find the maximum sum of consecutive zeros at the start and end of any string rotation. First, we will use a naïve approach to solve the problem, which generates all rotations of the binary string and counts the starting and ending consecutive zeros. After that, we will learn an optimized algorithm that counts the maximum consecutive zeros. Problem statement – Here, we have a string of size N containing only 0 and 1 characters. We need to find the maximum sum of consecutive zeros at the start and end of any ... Read More

Java Program to Implement Unrolled Linked List

Shubham Vora
Updated on 24-Aug-2023 18:10:04

217 Views

In this problem, we will learn to implement the unrolled linked list. The unrolled linked list is a specialized version of the linked list. The normal linked list contains a single element in a single node, but the unrolled linked list contains a group of elements in each node. Also, insertion, deletion, and traversal in the unrolled linked list work the same as the typical linked list. The linear search is faster in the array than in the linked list. So, we can add elements in the array and an array in each node of the linked list. Also, ... Read More

Java Program to Implement the Vizing's Theorem

Shubham Vora
Updated on 24-Aug-2023 18:07:17

134 Views

In this problem, we need to implement Vizing's Theorem. Vizing's Theorem is used with graphs. Theorem statement - For any undirected graph G, the value of the Chromatic index is equal to the d or d + 1, where d is the maximum degree of the graph. The degree for any vertex is the total number of incoming or outgoing edges. Problem statement - We have given a graph and need to implement Vizing's Theorem to find the Chromatic index of the graph. Note - The chromatic index is a positive integer, requiring a ... Read More

Java Program to Implement the Schonhage-Strassen Algorithm for Multiplication of Two Numbers

Shubham Vora
Updated on 24-Aug-2023 18:04:22

254 Views

The Schonhage-Strassen algorithm is useful when we need to multiply large decimal numbers. As Java supports the 1018 size of integers, and if we need to multiply the digits of more than 1018, we need to use the Schonhage-Strassen algorithm, as it is one of the fastest multiplication algorithms. It uses the basic rules for the multiplication of two numbers. It first performs the linear convolution and then performs the carry to get the final result. Problem statement - We have given mul1 and mul2 large decimal numbers and need to implement the Schonhage-Strassen algorithm to multiply both ... Read More

Java Program to Implement the RSA Algorithm

Shubham Vora
Updated on 31-May-2024 15:28:42

6K+ Views

The RSA name is given by their inventors which is used to encrypt the text with high security. The RSA technique is one of the most used techniques to encrypt text, as it is the asymmetric encryption algorithm. It encrypts the text by utilizing the mathematical properties of the prime numbers. In the RSA algorithm, the sender and receiver have private keys. Also, a common public key exists, which the sender shares with the receiver. The sender encrypts the plain text using their own public and private key, and the receiver decrypts the message using their private and public ... Read More

Java Program to Implement the Linear Congruential Generator for Pseudo Random Number Generation

Alshifa Hasnain
Updated on 29-Mar-2025 02:56:11

558 Views

In this article, we will implement the linear congruential generator for pseudo random number generation in Java. Pseudo random number generator (PRNG) are mainly used in simulations, cryptography, or mathematical tasks. What is an LCG? A Linear Congruential Generator (LCG) is a technique to generate a sequence of numbers that looks like random numbers but are actually determined. It is one of the reasons to call it a pseudo-random number. The Linear Congruential Generator (LCG) technique generates a random number based on the previous number and uses linear recurrence to generate the sequence of the random number. Mathematical Formula We can ... Read More

Java Program to Implement the String Search Algorithm for Short Text Sizes

Shubham Vora
Updated on 24-Aug-2023 17:56:59

162 Views

In this problem, we need to find the index of the pattern in the string. Implementing an efficient text search is very important to allow users to search large text databases easily. For example, you are writing a blog in Microsoft Word or code in VSCode, containing 1 lakh+ word. If the search algorithm is inefficient, it can take time to show you search results when searching for any word or sentence. We will learn two different approaches to implementing the string search algorithm. One is the naïve approach, and another is the KMP algorithm. Problem statement - ... Read More

Advertisements