Skip to content

Instantly share code, notes, and snippets.

@thmain
Created October 15, 2017 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/9c3654ff1a458402ccdfe33b400bcba2 to your computer and use it in GitHub Desktop.
Save thmain/9c3654ff1a458402ccdfe33b400bcba2 to your computer and use it in GitHub Desktop.
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