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:

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

Author: Amod Mahajan

My name is Amod Mahajan and I am an IT employee with 6+ years of experience in Software testing and staying in Bengaluru. My area of interest is Automation testing. I started from basics and went through so many selenium tutorials. Thanks to Mukesh Otwani as his tutorials are easy and cover basics to advance. I have habit of exploring concepts by deep diving. I used to make notes. I thought of sharing my knowledge through posts and now I am here. #KeepLearning #ShareLearning