Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • About Us
  • 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

Controlling Browser Driver Services In Selenium WebDriver Uncategorized
REST Assured Tutorial 14 –RequestSpecification – How the request will look like Uncategorized
PassBodyToPostReq – Make Selenium Easy Uncategorized
Understanding Calendars – Make Selenium Easy Uncategorized
apply method in Function Uncategorized
December 2, 2018 – Make Selenium Easy Uncategorized

Recent Posts

  • How to Handle NoSuchElementException Gracefully in Selenium
  • How to Handle StaleElementReferenceException in Selenium
  • Common Selenium Exceptions and How to Fix Them
  • How to Use WebDriverWait and Expected Conditions Effectively
  • Implicit Wait vs Explicit Wait vs Fluent Wait in Selenium

Recent Comments

No comments to show.

Archives

  • August 2026
  • July 2026
  • June 2026
  • May 2026
  • 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
  • Locators and Elements
  • Uncategorized
  • Waits and Synchronization

Copyright © 2026 Make Selenium Easy.

Powered by PressBook Masonry Dark