How to Remove Duplicates from a String in Java Using Regex ? Last Updated : 12 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In Java programming, Regex (regular expressions) is a very important mechanism. It can be used to match patterns in strings. Regular expressions provide a short and simple syntax for describing the patterns of the text. In this article, we will be learning how to remove duplicates from a String in Java using Regex. Regex Implementation:Pattern: It is a pre-defined class. It can be used to represent the compiled version of a regular expression. Matchers: It is a class, that is used to match the pattern against a given input string and provides methods for matching, finding, replacing, and other operations.Program to Remove Duplicates From a String in Java in RegexBelow is the Program to Remove Duplicates From a String in Java in Regex: Java // Java Program to remove duplicates from a String in Java in regex import java.util.regex.Matcher; import java.util.regex.Pattern; public class GFGRemoveDuplicates { //main method public static void main(String[] args) { String str = "Java programming"; // Use regex to remove duplicate characters String result = removeDuplicates(str); //print the original string System.out.println("Original String: " + str); //print the string after removing the duplicates System.out.println("String after removing duplicates: " + result); } private static String removeDuplicates(String input) { // Define a regex pattern to match any duplicated character String regex = "(.)(?=.*\\1)"; // Create a Pattern object Pattern pattern = Pattern.compile(regex); // Create a Matcher Matcher matcher = pattern.matcher(input); // Use replaceAll to remove duplicates return matcher.replaceAll(""); } } OutputOriginal String: Java programming String after removing duplicates: Jv poraming Explanation of the above Program:In the above java program, it demonstrates that how to remove the duplicate characters of the given string. It can be implements using Patter and Matchers. First define the regex expression to remove the duplicates((.)(?=.*\\1)) Then create the pattern object to compile the regex after that create the matcher that can be used to remove the duplicates characters into the program. Comment More infoAdvertise with us Next Article How to Remove Duplicates from a String in Java Using Regex ? S seepanarajvskq Follow Improve Article Tags : Java Java Programs Java-Strings Java-String-Programs strings Java Examples +2 More Practice Tags : JavaJava-StringsStrings Similar Reads How to Remove Duplicates from a String in Java Using Hashing? Working with strings is a typical activity in Java programming, and sometimes we need to remove duplicate characters from a string. Using hashing is one effective way to do this. By assigning a unique hash code to each element, hashing enables us to keep track of unique items. This article will exam 2 min read How to Remove Duplicates from a String in Java? Working with strings is a typical activity in Java programming, and sometimes we need to remove duplicate characters from a string. In this article we have given a string, the task is to remove duplicates from it. Example Input: s = "geeks for geeks"Output: str = "geks for" Remove Duplicates From a 2 min read How to Remove All Punctuation from a String using Regex in Java? In this article, we will explain how to remove all punctuation from a given String by using Java with the help of Regex. Here regex means regular expression. The regex is a powerful way to explain the search pattern. One more thing is that regular expressions are mostly used for Searching a required 4 min read How to Replace All Occurings of String Using Regex in Java? Regex in Java is an interesting way to search for patterns in a string that the user provides. Expanded as Regular Expressions, It consists of some patterns that can be planned and modified according to the usage in the program. Example of Replace All Occurings of a StringInput: str="This is a sampl 2 min read Java Program to Remove a Given Word From a String Given a string and a word that task to remove the word from the string if it exists otherwise return -1 as an output. For more clarity go through the illustration given below as follows. Illustration: Input : This is the Geeks For Geeks word="the" Output : This is Geeks For Geeks Input : Hello world 3 min read How to Replace the First Occurrence of a String Using Regex in Java? Regex is a very interesting way to search for patterns in a String that the user provides. Regex stands for Regular Expressions. It consists of some patterns that can be planned and modified according to the usage of the program. In this article, we will discuss how to replace the first occurrence o 2 min read Java Program to Remove Duplicate Entries from an Array using TreeSet Features of TreeSet is the primary concern it is widely used in remove duplicates in the data structure as follows: TreeSet implements the SortedSet interface. So, duplicate values are not allowed and will be leftovers.Objects in a TreeSet are stored in a sorted and ascending order.TreeSet does not 3 min read Java - Remove White Spaces from String using Regex The Regex is an expression that is used for searching a required pattern or matching a required pattern or Manipulating the input based on requirements by using regex. This Regex is available in java.util.regex.* package in Java Programming. In this article, we will learn to remove the white spaces 2 min read How to Split Strings Using Regular Expressions in Java? Split a String while passing a regular expression (Regex) in the argument and a single String will split based on (Regex), as a result, we can store the string on the Array of strings. In this article, we will learn how to split the string based on the given regular expression. Example of Split Stri 2 min read Java Program to Remove Duplicate Elements From the Array Given an array, the task is to remove the duplicate elements from an array. The simplest method to remove duplicates from an array is using a Set, which automatically eliminates duplicates. This method can be used even if the array is not sorted.Example:Java// Java Program to Remove Duplicate // Ele 6 min read Like