How to remove the duplicates From String in java?


Video is ready, Click Here to View ×

remove repeated characters from string java,
eliminate duplicate characters string java,
delete duplicate characters string java,
delete repeated characters string java,
remove duplicate characters in string using java,
java program to remove duplicate characters from string,
remove duplicate characters from string java

26 thoughts on “How to remove the duplicates From String in java?

  1. package com.practice;

    //program to remove duplicate from string and print that string
    import java.util.HashSet;
    import java.util.Set;

    public class RemoveDuplicateCharacter {
    public static void main(String[] args) {
    String s = "Pranav";
    char[] input = s.toCharArray();
    Set set = new HashSet();
    StringBuffer sf = new StringBuffer();
    for (char c : input) {
    boolean flag = set.add(c);
    if (flag) {

  2. Hi Uploader, Thanks you so much. It helped a lot.
    I think following might be the simpler way for the freshers to undestand

    String s="sathya";
    Character c=null;
    Set<Character> h=new LinkedHashSet<Character>();
    for(int i=0;i<s.length();i++)

    StringBuffer sb=new StringBuffer();
    for(Character ch:h)


    In this program I want remove the duplicates from the string
    The logic is using Set where duplicates are not allowed. If we use HashSet here elements will be inserted
    in Set based on the hashcode of the element. If we want to preserve the insertion order then we should go
    for LinkedHashSet where duplicates are not allowed and insertion order is preserved.
    Note here if we try to insert duplicate elements in the Set, add() method returns simply false thats it, we
    wont get any exception here
    @author Sathya

  3. Now i am taking below code. i am already replace nextline. i need i am removed str2 and str3 also.
    please give me the solution for this.

    public class ReplaceNextLine {
    public static void main(String a[]) {
    String str1 = "java";
    String str2 = "java1";
    String str3 = "java2";
    String str5 = str1 + "n" + str2 + "n" + str3;
    System.out.println("2nd exp :" + str5.replaceAll("\n", ""));

Leave a Reply

Your email address will not be published. Required fields are marked *