Created
October 15, 2017 16:44
-
-
Save thmain/68b39684d91c27d78d8b5e387c88485f 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.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