Skip to content

Make Selenium Easy

And Keep It That Way

  • Home
  • Share
  • Toggle search form

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

Posted on 03/21/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 without Using Collection Concept.

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

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

User Input – abcabcabcabc

Output: abc

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.
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) + "\"");
        }

}

Please enter the word to remove duplicate chars: abcabcabcabcabc You entered : “abcabcabcabcabc”

String after removing duplicate chars: “abc”

Uncategorized

Post navigation

Previous Post: Authentication vs Authorization – Who vs Who+What
Next Post: API Testing Tutorial Part 3 – Understand Terms URN , URL ,URI & API

Related Posts

REST Assured Tutorial 62 – How To Use Path or URL Parameters In Rest Assured Uncategorized
Frequently Asked Java Program 03: Java Program to check if any string is palindrome Using inbuilt Reverse method of Java Uncategorized
TestNG Tutorials 40: Groups of Groups or MetaGroups in TestNG | Make Selenium Easy Uncategorized
ResponsePost – Make Selenium Easy Uncategorized
image – Make Selenium Easy Uncategorized
September 26, 2017 – Make Selenium Easy 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