Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

3 ways: How to Find Duplicate Words in String in Java

In this tutorial, I will be sharing how to find duplicate words in String in Java. There are many ways to solve this problem. One easy way is by using the Collections.frequency() method. Another way is without using the Collections API. I will be sharing both of them. Let's dive deep into the topic:

Read Also:  String Interview Questions and Answers 

First, understand the problem statement by writing some examples:

Examples of Duplicate Words in String are:

Given String: "Java is a Programming Language. Python is also a programming language."
Duplicate words are : "is","a","programming","language." 
 
Given String: "Java has 50 keywords in total. Null, true, and false might seem like keywords but they are not in Java"  
Duplicate words are : "keywords", "in", "java"  

Java Program to find Duplicate Words in String


1. Using HashSet

In the below program I have used HashSet and ArrayList to find duplicate words in String in Java.

import java.util.*; 
 
public class JavaHungry {
public static void main(String args[]) {
// Given String containing duplicate words
String input = "Java is a programming language. Python is also a programming language.";
// Converting given String to lowerCase
input = input.toLowerCase();
/* Split the given String into words using
built-in split() method */
String[] strArray = input.split(" ");
/* Declare List of String that will
contain repeated words*/
ListString> repeatedWords = new ArrayList();
/* Declare HashSet of String that will
contain unique words */
HashSetString> uniqueWords = new HashSet();
for(String str : strArray)
{
if (!uniqueWords.add(str))
repeatedWords.add(str);
}
System.out.println(repeatedWords);
}
}

Output:
[is, a, programming, language.]

The above program can be much simpler by using a built-in Collections.frequency() method. We will see in the next step.

2. Using Collections.frequency() method

In the below program I have used Collections.frequency() method.

import java.util.*;
public class JavaHungry {
public static void main(String args[]) {
// Given String containing duplicate words
String input = "Java is a programming language. Python is also a programming language.";
// Converting given String to lowerCase
input = input.toLowerCase();
/* Split the given String into words using
built-in split() method */
String[] strArray = input.split(" ");
// Converting String array to List of String
ListString> listOfWords = Arrays.asList(strArray);
/* Declare HashSet of String that will
contain unique words */
HashSetString> uniqueWords = new HashSet(listOfWords);
for(String word : uniqueWords)
{
if(Collections.frequency(listOfWords,word) > 1)
System.out.print(" "+ word+" ");
}
}
}

Output:
 a  language.  is  programming  

3. Without Using Collections

In the below program we have printed the duplicate words in String without using Collections API.

import java.util.*;
public class JavaHungry {
public static void main(String args[]) {
// Given String containing duplicate words
String input = "Java is a programming language. Python is also a programming language.";
// Converting given String to lowerCase
input = input.toLowerCase();
/* Split the given String into words using
built-in split() method */
String[] words = input.split(" ");
int length = words.length;
// Using for loop
for( int i=0; i length; i++)
{
// Set count to 1 for every iteration
int count = 1;
for(int j=i+1; j length; j++)
{
if(words[i].equals(words[j]))
{
count++;
/* Setting words[j]="0" to avoid
visited words */
words[j] = "0";
}
}
if (words[i] != "0" && count > 1)
System.out.print(" "+words[i]+" ");
}
}
}

Output:
 is  a  programming  language.


That's all for today, please mention in comments in case you know any other method to find duplicate words in a String in Java.


This post first appeared on Java Hungry, please read the originial post: here

Share the post

3 ways: How to Find Duplicate Words in String in Java

×

Subscribe to Java Hungry

Get updates delivered right to your inbox!

Thank you for your subscription

×