Skip to content

Instantly share code, notes, and snippets.

@thmain
Created October 15, 2017 16:44
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/68b39684d91c27d78d8b5e387c88485f to your computer and use it in GitHub Desktop.
Save thmain/68b39684d91c27d78d8b5e387c88485f to your computer and use it in GitHub Desktop.
import java.util.Arrays;
public class removeDuplicatesUsingSorting {
public static String removeDuplicates(String s) {
char[] chars = s.toCharArray();
Arrays.sort(chars); // O(nlogn)
String sbString = new String();
for (int i = 1; i < chars.length; i++) {
if(chars[i]!=chars[i-1])
sbString +=chars[i];
}
//handle the first character
if(chars[0]!=chars[1])
sbString = chars[0] + sbString;
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