Created
October 15, 2017 16:45
-
-
Save thmain/c30f5cd51f0e2d1e094756b60d246ab4 to your computer and use it in GitHub Desktop.
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.util.HashSet; | |
import java.util.Iterator; | |
public class RemoveDuplicatesUsingSet { | |
public static String removeDuplicates(String s){ | |
HashSet<Character> set = new HashSet<Character>(); | |
char [] chars = s.toCharArray(); | |
for (int i = 0; i < chars.length; i++) { | |
set.add(chars[i]); | |
} | |
Iterator<Character> iterator = set.iterator(); | |
String sbString = new String(); | |
while (iterator.hasNext()) | |
sbString += iterator.next()+""; | |
return sbString; | |
} | |
public static void main(String[] args) { | |
String s = "tutorialhorizon"; | |
System.out.println(removeDuplicates(s)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment