Program to remove duplicate characters from a word in Java / How to remove duplicate characters from a word in Java

There are multiple approaches to accomplish this. Following post lists those.

Method 1 : Removing characters from original word

Iterate over the word entered by the user and in each iteration, add one character from the word to a java.lang.StringBufferand replace all the occurrences of this character in the word with a blank space. Before adding the character to the StringBuffer ensure to check that a blank is not added.

static void method1(){
	Scanner scanner = new Scanner(System.in);
	System.out.println("Please enter a word");
	String word = scanner.next();
	StringBuffer buffer = new StringBuffer();
	for (int i = 0; i < word.length(); i++) {
		char c = word.charAt(i);
                // check for an empty space
		if(c != ' ') {
		   buffer.append(c);
                }
                // replace all occurrences of this character with a space
		word = word.replace(c, ' ');
	}
	System.out.println(buffer);
        scanner.close();
}

Method 2 : Using a java.util.Set

This method utilizes the property of java.util.Setthat it can contain only unique elements. If an element matching the one which is already present in the set is added, it replaces the previous element with the new one.

Iterate over the word and in every iteration add its one character to the set. If the set would already have that character, then it would replace the new character with the older one and there would be only one occurrence of that character.

After the iteration of the word is completed, iterate over the set using a java.util.Iteratorand add its contents to a java.lang.StringBuffer. This iteration is required because if you directly print the contents of a set containing letters a,b,c it will print it as [a,b,c] which is not the required output.

Further the iteration is done using an iterator because a set does not have a method for index based access. That is, we cannot directly access an element of a set using some get(index)method

static void method2(){
	Set set = new LinkedHashSet();
	Scanner scanner = new Scanner(System.in);
	System.out.println("Please enter a word");
	String word = scanner.next();
	for (int i = 0; i < word.length(); i++) {
		set.add(word.charAt(i));
	}
	StringBuffer buffer = new StringBuffer();
	for (Iterator iterator = set.iterator(); iterator.hasNext();) {
		Character character = (Character) iterator.next();
		buffer.append(character);
	}
	System.out.println(buffer);
        scanner.close()
}

Method 3 : Using java.util.List

This method is similar to the above method. In this method we use a java.util.ArrayList instead of a set. As before, we iterate over the word and add its characters to a list. Before each add operation, check that the list already does not contain that character using contains()method of the list. After the iteration completes, the list contains all the unique characters.

Now iterate over the list and add its contents to a java.lang.StringBuffer. Since a list has method get()for index based access, we can iterate over it using a traditional for loop.

static void method3(){
	List list = new ArrayList();
	Scanner scanner = new Scanner(System.in);
	System.out.println("Please enter a word");
	String word = scanner.next();
	for (int i = 0; i < word.length(); i++) {
	      char c = word.charAt(i);
	      if(!list.contains(c))
	         list.add(c);
	      }
	StringBuffer buffer = new StringBuffer();
	for (int i = 0; i < list.size(); i++) {
		buffer.append(list.get(i));
	}
	System.out.println(buffer);
        scanner.close();
}

Let’s tweak in:

  1. java.util.LinkedHashSetand has been used in Method 2 as it maintains the order of insertion of elements. If we would have used java.util.HashSetthen the order of characters would not be same every time.
  2. java.util.ArrayListmaintains the order of insertion, that is, elements are retrieved in the same order in which they were inserted.
  3. In all the methods we have used a java.lang.StringBufferas when the characters are added or modified, same StringBuffer object is used. If we use java.lang.Stringinstead, then in each addition of a character, a new String object is created resulting in extra memory usage.
  4. java.lang.StringBuffershould be used when there are chances of multiple String manipulations.
  5. When callingreplace()on a String object, the result should be assigned back to the original (or some other) String object else the changes will not be reflected. For Example,
    String word = "codippa";
    word.replace('p', 'P');
    System.out.println(word); // This will print "codippa". Changes gone !!!
    //assign it back
    word = word.replace('p', 'P');
    System.out.println(word); // This will print "codiPPa"

    This is because String is immutable. At every change a new String object is created and it needs to be assigned back to some object.

Was the post helpful. Do let us know in the space provided below. Cheers !!!

0

2
Leave a Reply

Mark Your Impression

  Subscribe  
newest oldest most voted
Notify of
P. Ronaldo
Guest

Loved the let’s tweak in section !!!

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.