Write a java program to remove Duplicate Characters in String

Write a Program that removes duplicate characters from the string. Your program should read a sentence (string) as input from the user and return a string removing duplicate characters. Retain the first occurrence of the duplicate character. Assume the characters are case–sensitive.

 Input and Output Format

  • Input consists of a string with a maximum size of 100 characters.
  • The output consists of a single string.

Refer sample output for formatting specifications

Sample Input 1:

hi this is sample test

Sample Output 1:

hi tsample

Sample Input 2:

ABC DEF

Sample Output 2:

ABC DEF

Java program to remove Duplicate Characters in String

Let us see the steps to remove duplicate characters in a string:

  • Input a string from the user.
  • Call the getvalues() method with the input string.
  • Inside the method, first, convert the string to a character array using the toCharArray() method.
  • Then, create an empty string buffer. Also, create a LinkedHashSet of character type. (Here, LinkedHashSet is used to allow unique characters & it will remove duplicate characters)
  • Add all the characters to the LinkedHashSet.
  • Now, iterate over the LinkedHashSet, and keep adding characters and check if the space is encountered then just append it to string buffer and print them.
package testCases;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Scanner;
public class MainJava {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
getvalues(s1);
}
public static void getvalues(String s1) {
char a[] = s1.toCharArray();
StringBuffer sb = new StringBuffer();
LinkedHashSet<Character> hs = new LinkedHashSet<Character>();
for (int i = 0; i < a.length; i++) {
hs.add(a[i]);
}
Iterator<Character> itr = hs.iterator();
while (itr.hasNext()) {
char o = itr.next();
if (o != ' ')
;
{
sb.append(o);
}
}
System.out.println(sb);
}
}

Output

Duplicate Character Remove using for loop

Obtain a string and just remove duplicate characters in the string using for loop. Let us consider we have the string “old is gold”. Then after removing the duplicate it should print “old isg”.

 Input and Output Format

  • Input consists of a string with a maximum size of 100 characters.
  • The output consists of a string without duplicates.

Refer sample output for formatting specifications

Sample Input 1:

old is gold

Sample Output 1:

old isg

Program to remove duplicate characters in Java

Let us see some simple steps through which we can achieve this:

  • Input a string from the user.
  • Call the getvalues() method with the string.
  • Inside the method, convert the string to a character array using the toCharArray() method.
  • Initialize a temp variable.
  • Iterate over the character array, and check whether the character is present or not by matching each character with its consecutive. If a character is not present add it to the result.
package com.testcases;

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        getvalues(s1);
    }

    public static void getvalues(String s1) {
        int j;
        char a[] = s1.toCharArray();
        int temp =0;
        for(int i = 0;i<a.length;i++){
            for(j =0 ; j < i ; j++){
                if(a[i]==a[j]){
                    break;
                }
            }
            if(j==i){
                a[temp++]=a[i];
            }
        }
        System.out.println(String.valueOf(Arrays.copyOf(a,temp)));
    }
}

Output

Remove Duplicate characters in a string using Java 8

Obtain a string from the user and remove the repeating character using Java version 8 features. Let us use the stream concept of Java 8.

 Input and Output Format

  • Input consists of a string with a maximum size of 100 characters.
  • The output consists of a string without duplicates.

Refer sample output for formatting specifications

Sample Input 1:

helloworld

Sample Output 1:

helowrd

Program to remove duplicates using Java 8

Let us see the following steps:

  • Input string from the user.
  • Create an empty string builder.
  • Use the distinct() method to eliminate duplicates from the string.
  • At last append string to the string builder.
package com.testcases;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s1 = scanner.nextLine();
        StringBuilder stringBuilder = new StringBuilder();
        s1.chars().distinct().forEach(temp->stringBuilder.append((char) temp));
        String duplicate = stringBuilder.toString();
        System.out.println(duplicate);
    }

}

Output

Thus, in this way, this was all about removing duplicate characters in a string in Java.

Leave a Comment

Your email address will not be published.