Java Program to Find Duplicate Words in a Regular Expression Last Updated : 23 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report Given an Expression which is represented by String. The task is to find duplicate elements in a Regular Expression in Java. Use a map or set data structures for identifying the uniqueness of words in a sentence. Examples: Input : str = " Hi, I am Hritik and I am a programmer. " Output: I am Explanation: We are printing duplicate words in the given Expression. Input : str = " Ironman is alive. " Output: There is no Duplicate Word. Explanation: There are no duplicate words present in the given Expression. Approach 1: Get the Expression.Store all Words in an Array.Splitting word using regex '\\W'. (use of regex)Iterating in the array and storing words and all the number of occurrences in the Map.Now, In the Map, If the number of occurrences is more than 1 then we are printing the word.Below is the implementation of the above approach: Java // Find Duplicate Words in a Regular Expression in Java import java.util.*; public class GFG { public static void main(String[] args) { // we have a expression String expression = "Hi, I am Hritik and I am a programmer"; // splitting words using regex String[] words = expression.split("\\W"); // we are creating a Map for storing // strings and it's occurrence" Map<String, Integer> word_map = new HashMap<>(); // Here we are iterating in words array and // increasing it's occurrence by 1. for (String word : words) { if (word_map.get(word) != null) { word_map.put(word, word_map.get(word) + 1); } // if the word came once then occurrence is 1. else { word_map.put(word, 1); } } // creating a keyset of word_map Set<String> word_set = word_map.keySet(); // We are iterating in word set for (String word : word_set) { // if word matched then checking occurrence if (word_map.get(word) > 1) // here we are printing the duplicate words System.out.println(word); } } } OutputI amApproach 2: Get the Expression.Store all Words in an Array.Splitting word using regex '\\W'. (use of regex)Matching every word of the array with other words through iteration.If words matched then adding words in a Set because set removes the repeated words added by the flow of iteration.Finally, we are printing the set.Below is the implementation of the above approach: Java // Find Duplicate Words in a Regular Expression in Java import java.util.*; public class Main { public static void main(String[] args) { String expression = "Hi, I am Hritik and I am a programmer"; // splitting words using regex String[] words = expression.split("\\W"); // creating object of HashSet class implemented by Set<String> set = new HashSet<>(); // here we are iterating in Array for (int i = 0; i < words.length - 1; i++) { for (int j = 1; j < words.length; j++) { // if strings matched then adding strings in // Set because if we ad same string set will // remove one and we have only repeated // words. if (words[i].equals(words[j]) && i != j) { set.add(words[i]); } } } // here we are printing the set System.out.println(set); } } Output[I, am] Create Quiz Comment D depresseddeadpool Follow 1 Improve D depresseddeadpool Follow 1 Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like