Created
October 15, 2017 16:46
-
-
Save thmain/9c3654ff1a458402ccdfe33b400bcba2 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.Iterator; | |
import java.util.LinkedHashSet; | |
public class RemoveDuplicatesUsingLHM { | |
public static String removeDuplicates(String s) { | |
LinkedHashSet<Character> set = new LinkedHashSet<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