Remove all Duplicates from a given String

In this article, you will learn how to remove all duplicates from a given string in Java. To remove all duplicates from a given string in Java, you can create a new string that only contains the unique characters from the original string.

Here are a few different solutions in Java for removing all duplicate characters from a given string:

Solution-1:

In this program, we use a LinkedHashSet to store the unique characters in the input string. LinkedHashSet ensures that the order of the characters is preserved while eliminating duplicates. We iterate through the input string, adding each character to the uniqueChars set. Since LinkedHashSet does not allow duplicates, it will automatically filter out duplicate characters.

Finally, we construct a new string (result) from the unique characters in the LinkedHashSet and return it. The resulting string will have all duplicates removed while maintaining the order of the original characters.

package com.javacodepoint.string;

import java.util.LinkedHashSet;

public class RemoveDuplicatesFromString {
	public static String removeDuplicates(String input) {
		if (input == null || input.isEmpty()) {
			return input; // If the input is null or empty, return as it is.
		}

		// Use a LinkedHashSet to maintain the order of unique characters while removing
		// duplicates
		LinkedHashSet<Character> uniqueChars = new LinkedHashSet<>();

		// Iterate through the string and add each character to the LinkedHashSet
		for (char c : input.toCharArray()) {
			uniqueChars.add(c);
		}

		// Build a new string from the unique characters in the LinkedHashSet
		StringBuilder result = new StringBuilder();
		for (char c : uniqueChars) {
			result.append(c);
		}

		return result.toString();
	}

	public static void main(String[] args) {
		String inputString = "exampletext";
		String withoutDuplicates = removeDuplicates(inputString);

		System.out.println("Original String: " + inputString);
		System.out.println("String without duplicates: " + withoutDuplicates);
	}
}

OUTPUT:

Original String: exampletext
String without duplicates: example

Solution-2:

there are other approaches to remove duplicates from a given string in Java. One common approach is to use a StringBuilder and iterate through the input string, appending each character to the StringBuilder only if it hasn’t been appended before.

In this approach, we use an boolean array visitedChars to keep track of characters that have been visited before. The size of the array is set to 256, assuming that we are dealing with ASCII characters (0 to 255). If you are dealing with Unicode characters, you might need to use a larger array or a different data structure like a HashSet<Character>.

We then iterate through the input string, and for each character c, we check if it has been visited before (visitedChars). If it hasn’t, we append it to the StringBuilder (result) and mark it as visited by setting visitedChars to true.

The resulting StringBuilder will contain only the unique characters from the input string. We then return this StringBuilder converted to a string using toString().

package com.javacodepoint.string;

import java.util.LinkedHashSet;

public class RemoveDuplicatesFromString2 {
	
	public static String removeDuplicates(String input) {
		if (input == null || input.isEmpty()) {
			return input; // If the input is null or empty, return as it is.
		}

		StringBuilder result = new StringBuilder();
		boolean[] visitedChars = new boolean[256]; // Assuming ASCII characters (0 to 255)

		for (char c : input.toCharArray()) {
			if (!visitedChars[c]) {
				result.append(c);
				visitedChars[c] = true;
			}
		}

		return result.toString();
	}

	public static void main(String[] args) {
		String inputString = "Javacodepoint";
		String withoutDuplicates = removeDuplicates(inputString);

		System.out.println("Original String: " + inputString);
		System.out.println("String without duplicates: " + withoutDuplicates);
	}
}

OUTPUT:

Original String: Javacodepoint
String without duplicates: Javcodepint

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *