Found 2620 Articles for Java

Frequency Measuring Techniques for Competitive Programming

Avinash Gupta
Updated on 28-Aug-2023 18:17:21

157 Views

In this article, we are going to find the different ways to find the frequency of numbers present in an array []. These methods are very useful in doing competitive programming for different problems for different cases. Sometimes, calculating the frequency of elements whether it is numbers or alphabets presented in the array is a complicated task. Various algorithms like Searching, array, divide and conquer can be used to find the repeated elements defined in the array. Note- Take an integer array. Let's explore the article, to know how it can be solved by using Java programming ... Read More

Smallest string divisible by two given Strings

Vanshika Sood
Updated on 27-Oct-2023 16:00:53

644 Views

The objective of this article is to determine the smallest string that is a multiple of both given strings. An interesting observation to note is that for two given strings s and t, the string s is a multiple of t if and only if s can be formed by repeating t one or more times. We have to find the smallest such string. Problem Statement Given two non-empty strings, s1 and s2, with lengths n and m respectively, the objective is to determine the smallest string that is a multiple of both s1 and s2. A ... Read More

How to Print Colored Text in Java Console?

Adeeba Khan
Updated on 25-Aug-2023 17:26:18

7K+ Views

When dealing with Java terminal apps, individuals may frequently want to print colored text to improve the output's aesthetic appeal and readability. ANSI escape codes can be used to generate colored text instead of the monochrome output that Java's default terminal generally produces. When printed to the console, ANSI escape codes are unique sets of characters that alter the text's appearance by altering its color, style, or background. In this article, we'll look at how to print colored text in the Java console using ANSI escape codes. We'll go over two examples, one with colored text only and the other ... Read More

How to Print an Array in Java Without using Loop?

Adeeba Khan
Updated on 25-Aug-2023 17:24:35

1K+ Views

The task of printing an array's elements in Java is one that programmers come upon regularly. Having a simple and effective technique for printing arrays is crucial, whether you want to display the array contents for debugging purposes, present them to the user in a prepared manner, or analyze the data within the array. While utilizing a loop is the most common and conventional strategy, there may be times when you need to look into other options to do the same objective. To provide you with a new viewpoint on how to handle array printing chores, this article seeks to ... Read More

How to Print all Keys of the LinkedHashMap in Java?

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

486 Views

Java's LinkedHashMap data structure combines the strengths of a HashMap as well as of a doubly-linked list. It is one of the better options for situations requiring predictable iteration as it not only offers key-value mapping like a HashMap but also preserves the insertion order of components. There are times when we need to print every key found in a LinkedHashMap for multiple reasons, that include debugging, analysis and information display to users. In this post, we will examine two excellent techniques that let us print every key from a LinkedHashMap in Java, enabling us to effectively extract and visualize ... Read More

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

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

2K+ 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

176 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

69 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

60 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

86 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

Advertisements