Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,888 questions

51,814 answers

573 users

How to remove duplicates from a string in Java

5 Answers

0 votes
import java.util.LinkedHashSet;
import java.util.Set;

class Program {
    static String removeDuplicates(String str) {
        Set<Character> lhs = new LinkedHashSet<Character>();
        
        char[] chars = str.toCharArray();
        for (char ch : chars) {
            lhs.add(ch);
        }
 
        StringBuilder sb = new StringBuilder();
        for (Character ch : lhs) {
            sb.append(ch);
        }   

        return sb.toString();
    }
    
    public static void main(String[] args) {
        String str = "abcdaaaabbbccccddddefghhhhhhxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        
        System.out.println(removeDuplicates(str));
    }
}



/*
run:

abcdefghx

*/

 



answered Feb 8, 2019 by avibootz
edited Feb 9, 2024 by avibootz
0 votes
import java.util.LinkedHashSet;
import java.util.Set;

class Program {
    static Set<Character> removeDuplicates(String str) {
        Set<Character> lhs = new LinkedHashSet<Character>();
         
        for (char ch:str.toCharArray()) {
            lhs.add(Character.valueOf(ch));
        }

        return lhs;
    }
    
    public static void main(String[] args) {
        String str = "abcdaaaabbbccccddddefghhhhhhxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        
        System.out.println(removeDuplicates(str));
    }
}



/*
run:

[a, b, c, d, e, f, g, h, x]

*/

 



answered Feb 8, 2019 by avibootz
edited Feb 9, 2024 by avibootz
0 votes
import java.util.LinkedHashSet;
import java.util.Iterator;
import java.util.Set;

class Program {
    static Set<Character> removeDuplicates(String str) {
        Set<Character> lhs = new LinkedHashSet<Character>();
         
        for (char ch:str.toCharArray()) {
            lhs.add(Character.valueOf(ch));
        }

        return lhs;
    }
    
    public static void main(String[] args) {
        String str = "abcdaaaabbbccccddddefghhhhhhxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        
        Set<Character> lhs = removeDuplicates(str);
        
        Iterator it = lhs.iterator();
        while(it.hasNext()) {
            System.out.print(it.next() + " ");
        }
    }
}



/*
run:

a b c d e f g h x 

*/

 



answered Feb 8, 2019 by avibootz
edited Feb 9, 2024 by avibootz
0 votes
import java.util.LinkedHashSet;
import java.util.Iterator;
import java.util.Set;

class Program {
    static Set<Character> removeDuplicates(String str) {
        Set<Character> lhs = new LinkedHashSet<Character>();
         
        for (char ch:str.toCharArray()) {
            lhs.add(Character.valueOf(ch));
        }

        return lhs;
    }
    
    public static void main(String[] args) {
        String str = "abcdaaaabbbccccddddefghhhhhhxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
        
        Set<Character> lhs = removeDuplicates(str);
        
        str = "";
        Iterator it = lhs.iterator();
        while(it.hasNext()) {
            str += it.next();
        }    
         
        System.out.println(str);
    }
}



/*
run:

abcdefghx

*/

 



answered Feb 8, 2019 by avibootz
edited Feb 9, 2024 by avibootz
0 votes
import java.util.stream.Collectors;
import java.util.Arrays;
 
class Program {
    static String removeDuplicates(String str) {
        String noDuplicates = Arrays.stream(str.split(""))
                            .distinct()
                            .collect(Collectors.joining());
                         
        return noDuplicates;
    }
     
    public static void main(String[] args) {
        String str = "abcdaaaabbbccccddddefghhhhhhxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
         
        System.out.println(removeDuplicates(str)); 
    }
}
 
 
 
/*
run:
 
abcdefghx
 
*/

 



answered Feb 9, 2024 by avibootz
edited Feb 9, 2024 by avibootz

Related questions

1 answer 52 views
1 answer 107 views
2 answers 201 views
1 answer 126 views
126 views asked Apr 20, 2020 by avibootz
1 answer 121 views
1 answer 165 views
...