Frequently Asked Java Program 29: Java Program to Remove Duplicate Characters From Word Without Using Collection Concept

Hello Folks,

As part of Frequently Asked Java Programs In Interviews For Freshers And Experienced, in this post we will see a java program to Remove Duplicate Characters From Word without Using Collection Concept.

We have already seen above program using Collection concept. But interviewer might ask to solve it without using Collection APIs.

Problem Statement:

Remove all duplicate characters from given word. Keep the order of char as it appears.

User Input – abcabcabcabc

Output: abc

Problem Solution:

The simplest solution will be – Construct a new String by reading input string char by char. Add only if new string does not contain incoming char.

Logic in steps:

  1. Convert given string in to a char array.
  2. Create an empty string. You can use String or StringBuilder or StringBuffer class.
  3. I will use StringBuilder class. Remember StringBuilder class does not have contains() method.
  4. I will iterate char array and append to resultant string if it does not contain specific char.
  5. AT the end, my resultant string will have only unique characters.

Java Program:

package StringPrograms;

import java.util.LinkedHashSet;
import java.util.Scanner;

public class RemoveDuplicatesCharFromWordWithoutUsingCollections {

	public static String removeDuplicateCharFromGivenWord(String inputWord) {
		/*
		 * Since we need to check for duplicate chars in word, we must need to extract
		 * each char of word. And the best way is to convert in to char array.
		 */
		inputWord = inputWord.toLowerCase();
		char charsOfWord[] = inputWord.toCharArray();

		// Since we need to modify content of String, we will create an empty string using StringBuilder
		StringBuilder stringWithoutDuplicateChar = new StringBuilder();
		for(char c : charsOfWord)
		{
			// StringBuilder does have contains method. We can use indexOf method as an alternative 
			if(stringWithoutDuplicateChar.indexOf(Character.toString(c)) == -1)
			{
				stringWithoutDuplicateChar = stringWithoutDuplicateChar.append(c);
			}
		}
		
		return stringWithoutDuplicateChar.toString();

	}

	public static void main(String[] args) {

		// User input to trim spaces
		Scanner sc = new Scanner(System.in);
		System.out.println("Please enter the word to remove duplicate chars:");

		String userInput = sc.nextLine();

		sc.close();

		System.out.println("You entered : \"" + userInput + "\"");

		System.out.println(
				"String after removing duplicate chars: \"" + removeDuplicateCharFromGivenWord(userInput) + "\"");
	}

}

Output:

Please enter the word to remove duplicate chars:
abcabcabcabcabc
You entered : “abcabcabcabcabc”
String after removing duplicate chars: “abc”

=====================================================

Please enter the word to remove duplicate chars:
abc
You entered : “abc”
String after removing duplicate chars: “abc”

#HappyCoding

Leave a Reply

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