Programming Articles - Page 1870 of 3366

Find the second most repeated word in a sequence in Java

AmitDiwan
Updated on 08-Jul-2020 10:20:18

684 Views

To find the second most repeated word in a sequence in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo{    static String second_repeated(Vector my_seq){       HashMap my_map = new HashMap(my_seq.size()){          @Override          public Integer get(Object key){             return containsKey(key) ? super.get(key) : 0;          }       };       for (int i = 0; i < my_seq.size(); i++)       my_map.put(my_seq.get(i), my_map.get(my_seq.get(i))+1);       int first_val = Integer.MIN_VALUE;       int sec_val ... Read More

Find the first repeated word in a string in Java

AmitDiwan
Updated on 08-Jul-2020 10:17:39

962 Views

To find the first repeated word in a string in Java, the code is as follows −Example Live Demoimport java.util.*; public class Demo{    static char repeat_first(char my_str[]){       HashSet my_hash = new HashSet();       for (int i=0; i

Find the uncommon values concatenated from both the strings in Java

AmitDiwan
Updated on 08-Jul-2020 10:16:20

280 Views

To find the uncommon values concatenated from both the strings in Java, the code is as follows −Example Live Demoimport java.util.*; import java.lang.*; import java.io.*; public class Demo{    public static String concat_str(String str_1, String str_2){       String result = "";       int i;       HashMap my_map = new HashMap();       for (i = 0; i < str_2.length(); i++)       my_map.put(str_2.charAt(i), 1);       for (i = 0; i < str_1.length(); i++)       if (!my_map.containsKey(str_1.charAt(i)))       result += str_1.charAt(i);       else       ... Read More

Java program to display Hostname and IP address

AmitDiwan
Updated on 28-Aug-2024 21:18:08

4K+ Views

In this article, we will learn to display the Hostname and IP address using Java. To display the Hostname and IP address we will be using the InetAddress class from the java.net package. We’ll write a simple program to fetch and print this information, and perform the exception handling to catch the exception if the data isn't found. Problem Statement Write a program in Java to display the Hostname and IP address. Below is a demonstration of the same − Output  The IP address is : 127.0.0.1The host name is : jdoodle Steps to display Hostname and IP address Following are ... Read More

Java program to find Maximum and minimum element’s position in a list

AmitDiwan
Updated on 07-Jul-2020 09:54:25

406 Views

To find the maximum and minimum element’s position in a list, the Java program is as follows −Example Live Demoimport java.util.*; import java.util.Arrays; import java.util.Collections; public class Demo{    public static int index_val(int my_arr[], int t){       if (my_arr == null){          return -1;       }       int len = my_arr.length;       int i = 0;       while (i < len){          if (my_arr[i] == t){             return i;          } else {         ... Read More

Program to Iterate over a Stream with Indices in Java 8

AmitDiwan
Updated on 07-Jul-2020 09:52:03

623 Views

To iterate over a Stream with Indices in Java 8, the code is as follows −Example Live Demoimport java.util.stream.IntStream; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; public class Demo{    public static void main(String[] args){       String[] my_array = { "T", "h", "i", "s", "s", "a", "m", "p", "l", "e" };       AtomicInteger my_index = new AtomicInteger();       System.out.println("The elements in the string array are :");       Arrays.stream(my_array).map(str -> my_index.getAndIncrement() + " -> " + str).forEach(System.out::println);    } }OutputThe elements in the string array are : 0 -> T 1 -> h 2 -> i 3 ... Read More

Java program to print unique values from a list

AmitDiwan
Updated on 08-Nov-2024 22:30:18

1K+ Views

In this article, we will learn to print unique values from a List in Java. This program will use a loop-based approach to identify and display only the values that appear once, skipping any duplicates. This approach is helpful when working with datasets where you want to remove repeated items and focus on distinct entries. Problem Statement Write a program in Java to print unique values from a list. Below is a demostration of the same − Input 55, 67, 99, 11, 54, 55, 88, 99, 1, 13, 45 Output The distinct elements in the array are 55 67 99 11 ... Read More

Java program to find all close matches of input string from a list

Alshifa Hasnain
Updated on 17-Dec-2024 03:34:20

759 Views

Finding close matches to a string from a list of words is a common problem in string manipulation and pattern recognition. This article demonstrates two effective approaches for solving this problem in Java. The first approach utilizes string encoding to identify similar patterns, while the second approach leverages the Levenshtein Distance algorithm to find approximate matches. String Encoding Approach The string encoding approach is a clever technique where each string is transformed into a unique encoded format. Strings with the same encoded pattern are considered matches HashMap A HashMap in Java is a collection that stores key-value pairs, where keys ... Read More

Java program to remove all duplicates words from a given sentence

AmitDiwan
Updated on 23-Oct-2024 17:29:44

1K+ Views

In this article, we will learn to remove duplicate words from a given sentence in Java. The first method uses Java Streams to remove duplicates and join the words back into a sentence using the distinct() and Collectors.joining() methods. The second method uses a Set to automatically filter out duplicate words and a StringJoiner to combine the words back into a sentence. Both methods will give a sentence with only unique words. Different approaches Following are the different approaches to remove all duplicate words from a given sentence − Using Java Streams ... Read More

Java program to sort words of sentence in ascending order

AmitDiwan
Updated on 07-Jul-2020 09:41:56

5K+ Views

To sort words of sentence in ascending order, the Java code is as follows −Example Live Demoimport java.util.*; public class Demo{    static void sort_elements(String []my_str, int n){       for (int i=1 ;i= 0 && temp.length() < my_str[j].length()){             my_str[j+1] = my_str[j];             j--;          }          my_str[j+1] = temp;       }    }    public static void main(String args[]){       String []my_arr = {"This", "is", "a", "sample"};       int len = my_arr.length;       sort_elements(my_arr,len);       System.out.print("The sorted array is : ");       for (int i=0; i

Advertisements