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 Using Collection Concept.

Problem Statement:

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

User Input – abcabcabcabc

Output: abc

Problem Solution:

Step by step logic:

  1. Since we need to find duplicate characters in a word, we need to extract all characters from word. We can use toCharArray() method of String class to get an array of chars of words.
  2. We need to store chars in a container who does not allow duplicates and maintains insertion order. We can use LinkedHashSet container for the same.
  3. Iterate char array and add each char in to LinkedHashSet container . After iteration, container will have only single occurrence of each char.
  4. Now we need to form a string using chars of container. We can use StringBuilder or StringBuffer for the same.

Java Program:

Output:

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

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

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

You can run above program for multiple inputs and if it fails for any condition, let me know.

#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

Leave a Reply