Created
July 10, 2018 12:43
-
-
Save harishkotra/e84937bcdb22d1b35b6b989409bd9908 to your computer and use it in GitHub Desktop.
Design an algorithm and write code to remove the duplicate characters in a string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.util.*; | |
public class RemoveDuplicates{ | |
public static void main(String []args){ | |
String s = "aabnsbsbsssbbsbsbs"; | |
char[] characters = s.toCharArray(); | |
Set<Character> newSet = new LinkedHashSet<Character>(); | |
for(char c: characters){ | |
newSet.add(c); | |
} | |
StringBuilder sb = new StringBuilder(); | |
for(Character charac: newSet){ | |
sb.append(charac); | |
} | |
String newString = sb.toString(); | |
System.out.println(newString); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment