Tuesday, 4 April 2017

Java Program to remove duplicates character from String


Sample Program:
 import java.util.LinkedHashSet;  
 import java.util.Set;  
 public class RemoveStringsDuplicates {  
      public static void main(String[] args) {  
           String string = "abcdanbcd";  
           // Convert String into char array.  
           char[] chars = string.toCharArray();  
           // Set is used to remove duplicates.  
           Set<Character> charSet = new LinkedHashSet<Character>();  
           for (char c : chars) {  
             charSet.add(c);  
           }  
           StringBuilder result = new StringBuilder(charSet.size());  
           for (Character character : charSet) {  
                result.append(character);  
           }  
           System.out.println(result.toString());  
      }  
 }  

Output:
 abcdn  

Enjoy Learning.

No comments:

Post a Comment