Home » Java program to remove duplicate characters from a string

Java program to remove duplicate characters from a string

by Online Tutorials Library

Java program to remove duplicate characters from a string

Many times we need to remove the duplicate characters from a string in Java. We can remove the duplicate characters from a string by using the simple for loop, sorting, hashing, and IndexOf() method. So, there can be more than one way for removing duplicates.

  1. By using the simple for loop.
  2. By using the sorting algorithm.
  3. By using the hashing.
  4. By using the indexOf() method.

Java program to remove duplicate characters from a string

Let’s understand each method of removing duplicate characters from a string.

1) By using for loop

It is the simplest way of removing duplicates characters from a string. We should have to use the following steps for removing duplicates.

  1. In the first step, we have to convert the string into a character array.
  2. Calculate the size of the array.
  3. Call removeDuplicates() method by passing the character array and the length.
  4. Traverse all the characters present in the character array.
  5. Check whether the str[i] is present before or not. If it is not present before, add it to the result.

Let’s implement the above theory’s code to understand how this method works to remove duplicates from a string.

RemoveDuplicatesExample1.java

Output

Java program to remove duplicate characters from a string

2) By using sorting

Another way of removing duplicate characters is by using the sorting algorithm. It is faster than the first method. In order to remove the duplicate characters from the string, we have to follow the following steps:

  1. First, we need to sort the elements.
  2. After that, we remove duplicates by comparing the current character with the previous character in a loop.
  3. In the end, we need to remove the extra characters at the end of the resultant string.

The method doesn’t maintain the original order of the input string. After removing the duplicates, the order can be changed from the original order.

Let’s implemet the above steps in a Java program.

RemoveDuplicatesExample2.java

Output

Java program to remove duplicate characters from a string

3) By using hashing

Another way of removing duplicate characters from a string is hashing. By using hashing, we can do that easily. We will use the following steps to remove duplicates by using hashing:

  1. In the main() method, we will create a string from which we have to remove duplicate characters.
  2. We will call the removeDuplicates() method and passed the string from which we want to remove duplicates.
  3. In the removeDuplicates() method, we will create the LinkedHashSet of character type. LinkedHashSet contains a distinct element. So, if we add each character of a string into LinkedHashSet, all duplicate characters are removed.
  4. In the end, we print all the characters of the LinkedHashSet.

Note: It maintains the insertion order of the characters in the string.

Let’s implement the above theory’s code to understand how actually hashing is used to remove duplicate characters in a string.

RemoveDuplicatesExample3.java

Output

Java program to remove duplicate characters from a string

4) By using indexOf() method

The last way of removing duplicate characters from a string is by using the indexOf() method. In this method, we will work on the index position of the character. In order to remove the duplicate character from the string, we will use the following steps:

  • In the main() method, we will create a string from which we have to remove duplicate characters.
  • We will call the removeDuplicates() method and passed the string from which we need to remove duplicate characters.
  • In the removeDuplicates() method, we will create a new empty string and calculate the original string’s length.
  • We will traverse the string using the loop and check for repeating characters using the indexOf() method.
  • The indexOf() method returns -1 when the element is not present. So, when the string doesn’t contain that character, we will add it to the empty string.

Note: It maintains the insertion order of the characters in which they are added in the string.

Let’s implement the above steps in a Java program.

RemoveDuplicatesExample4.java

Output

Java program to remove duplicate characters from a string

All the above-discussed methods are used for removing duplicate characters from the string. All the methods are easy to understand, and we can use any one of them to remove duplicates characters.


You may also like