Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/16/2025 By admin

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.

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

User Input – abcabcabcabc

Output: abc

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:

package StringPrograms; import java.util.LinkedHashSet;
import java.util.Scanner; public class RemoveDuplicatesCharFromWord { 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 remove duplicates and maintain the insertion order as well, * we will use LinkedHashSet with Generic as Character. */ LinkedHashSet wordWithoutDuplicate = new LinkedHashSet(); /* * Iterate char array and add in to container. Duplicate will not be added. */ for (Character c : charsOfWord) wordWithoutDuplicate.add(c); /* * We need to create word by appending all unique characters in sequence. SInce * String is immutable, we need to use StringBuilder or StringBuffer. */ StringBuilder finalOutput = new StringBuilder(); for (Character ch : wordWithoutDuplicate) finalOutput.append(ch); // Returning final output return finalOutput.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: abcabcabcabc You entered : “abcabcabcabc”

String after removing duplicate chars: “abc”

Uncategorized

Post navigation

Previous Post: Test Your Basics of Selenium WebDriver – Java By Answering These Interview Questions
Next Post: Advanced TestNG Tutorials 35: How To Pass Multiple Group Names to be Run at Runtime in TestNG XML Using Beanshell

Related Posts

REST Assured Tutorial 30 – How To Create POJO Classes Of A JSON Array Payload Uncategorized
Make Selenium Easy – Page 51 of 51 – Uncategorized
Stream api in java Uncategorized
image – Make Selenium Easy Uncategorized
fluentwait Uncategorized
fluent wait in selenium Uncategorized

Recent Posts

  • Getting Started with Selenium 4: What Is New and How to Upgrade from Selenium 3
  • Manual Testing
  • Baby Steps To Become Efficient Selenium-Java Automation Tester
  • Features of Selenium 4.0.0 Release – Java Binding
  • Part 1: Handling Drop-down Created Using SELECT Tag In Selenium

Recent Comments

No comments to show.

Archives

  • April 2026
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • April 2024
  • March 2024
  • February 2024
  • December 2023
  • October 2023
  • August 2023
  • November 2022
  • September 2022
  • August 2022
  • July 2022
  • May 2022
  • March 2022
  • October 2021
  • April 2021
  • March 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • May 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • January 2018

Categories

  • Getting Started
  • Uncategorized

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark