Java Program to Find Duplicate Words in a String - 3 Ways

In this section, we will show you three different ways to find duplicate words in a given String in Java.

1. Using For loop and Map 

2. Using Java 8 Streams

3. Using Google Guava MultiSet

Example 1: Using For loop and Map 

import java.util.HashMap;
import java.util.Map;

public class Main {

public static void main(String[] args) {

String string = "Java Python Go C# Java Python Java Go C";

//Split the string into words based on whitespace
String words[] = string.split(" ");


Map<String, Integer> occurrences = new HashMap<>();

//Iterate through String array
for ( String word : words ) {

Integer oldCount = occurrences.get(word);
if ( oldCount == null ) {
oldCount = 0;
}
occurrences.put(word, oldCount + 1);
}

for (Map.Entry<String,Integer> entry : occurrences.entrySet())
if(entry.getValue()>1){
System.out.println(entry.getKey());
}
}
}

1. split() method in Java splits a string into substrings using a delimiter that is specified using a regular expression. Here delimiter is whitespace.

2. Then we iterate over String array and put the word and word count inside HashMap.

3. Finally iterate the Map and print the elements whose value is greater than one.

Console Output: 

Java

Go

Python


Example 2: Using Java 8 Streams

import java.util.*;
import java.util.stream.Collectors;

public class Main {

public static void main(String[] args) {

String string = "Java Python Go C# Java Python Java Go C";

List<String> wordsList = Arrays.stream(string.split(" "))
.collect(Collectors.toList());

Set<String> duplicatingStrings =
wordsList.stream().filter(i -> Collections.frequency(wordsList, i) > 1)
.collect(Collectors.toSet());

System.out.println(duplicatingStrings);

}
}

1. split() method in Java splits a string into substrings using a delimiter that is specified using a regular expression. Here delimiter is whitespace. It returns an array of String. 

2. toList() returns a Collector that accumulates the input elements into a new List.

3. stream() method accepts a mandatory parameter array which is the array of whose elements are to be converted into a sequential stream and returns a Sequential Stream from the array passed as the parameter.

4. The Java stream filter() method allows us to narrow down the stream elements based on a criterion. Here we are filtering the words whose count is more than one.

5. toSet() method is used to collect a stream into a set.

Console Output: 

[Java, Go, Python]


Example 3. Using Google Guava MultiSet

First add Google Guava to your project: https://mvnrepository.com/artifact/com.google.guava/guava
import com.google.common.collect.HashMultiset;
import com.google.common.collect.Multiset;

public class Main {

public static void main(String[] args) {

String string = "Java Python Go C# Java Python Java Go C";
String []words = string.split(" ");

Multiset<String> frequencies = HashMultiset.create();
for (String word : words) {
frequencies.add(word);
}

for (String word : frequencies.elementSet()) {
if(frequencies.count(word)>1){
System.out.println(word);
}
}

}
}

A Multiset is an collection that is kindred to Set, but it may have duplicate elements. The duplicate elements are fortified by maintaining a count of the total number of times an element appears in the collection.

Console Output: 

Java

Go

Python

More Interesting topics,

 

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete