How to Remove Duplicates from a String in Java Using Hashing? Last Updated : 12 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 examine the use of hashing in Java to eliminate duplicates from a string. ApproachPreserving a set of unique characters is the primary motivation for using hashing to eliminate duplicates from a string. The hash code for each character in the string may be found by iterating over it and storing unique characters in a hash set. We effectively filter out duplicates by doing this. Example to Remove Duplicates from a String in Java Using HashingLet's look at a simple example in which we want to use hashing to eliminate duplicates from the term "programming". Java // Java Program to Remove Duplicates // From a String in Java Using Hashing import java.util.HashSet; // Driver Class public class RemoveDuplicatesExample { public static String removeDuplicates(String input) { HashSet<Character> uniqueChars = new HashSet<>(); StringBuilder result = new StringBuilder(); for (char ch : input.toCharArray()) { if (uniqueChars.add(ch)) { // Character is unique, // add it to the result result.append(ch); } } return result.toString(); } public static void main(String[] args) { String inputString = "programming"; String result = removeDuplicates(inputString); System.out.println("Original String: " + inputString); System.out.println("String after removing duplicates: " + result); } } OutputOriginal String: programming String after removing duplicates: progamin Explanation of the above Program:The removeDuplicates function stores unique characters observed in a HashSet (uniqueChars) after receiving a string as input.It uses add() to run over each character in the input text and determine whether or not it already exists in the HashSet.The character is added to the StringBuilder result if it is unique.Converting the StringBuilder to a string yields the desired outcome. Comment More infoAdvertise with us Next Article How to Efficiently Remove Duplicates from an Array without using Set? R rahul9yw89 Follow Improve Article Tags : Java Java Programs Java-Strings strings Java Examples +1 More Practice Tags : JavaJava-StringsStrings Similar Reads How to Remove Duplicates from a String in Java Using Regex ? 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 J 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 Duplicate Elements from the Vector in Java? Using LinkedHashSet and TreeSet, duplicate elements are removed. Because the LinkedHashSet and TreeSet do not accept duplicate elements. Example: Input : vector = [1, 2, 3, 4, 2, 4] Output: vector = [1, 2, 3, 4] Input : vector = [a, b, a, c, d, a] Output: vector = [a, b, c, d]Approach 1: Using Linke 3 min read How to find duplicate elements in a Stream in Java Given a stream containing some elements, the task is to find the duplicate elements in this stream in Java. Examples: Input: Stream = {5, 13, 4, 21, 13, 27, 2, 59, 59, 34} Output: [59, 13] Explanation: The only duplicate elements in the given stream are 59 and 13. Input: Stream = {5, 13, 4, 21, 27, 5 min read How to Efficiently Remove Duplicates from an Array without using Set? Arrays are a fundamental data structure in Java that stores data of the same type in contiguous memory locations. Removing duplicate elements from an array is a common operation that can be easily accomplished using sets. However, in this article, we will learn how to remove duplicates from an array 2 min read How to Convert Comma Separated String to HashSet in Java? Given a Set of String, the task is to convert the Set to a comma-separated String in Java. Examples: Input: Set<String> = ["Geeks", "ForGeeks", "GeeksForGeeks"] Output: "Geeks, For, Geeks" Input: Set<String> = ["G", "e", "e", "k", "s"] Output: "G, e, e, k, s" There are two ways in which 2 min read Like