tag:blogger.com,1999:blog-8712770457197348465.post4013501979296302615..comments2024-03-28T02:47:19.159-07:00Comments on Javarevisited: How to Remove Duplicate Characters from String in Java? Examplejavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger21125tag:blogger.com,1999:blog-8712770457197348465.post-13987281818488214152020-12-14T20:45:29.524-08:002020-12-14T20:45:29.524-08:00Example 1:
input1: 153 input2: jaya
Output: “jy1...Example 1:<br /> input1: 153 input2: jaya<br /> Output: “jy1”<br /> 153 is armstrong number<br /> remove duplicates occurring more than once (a will be removed)<br /> diff in digits: 5-3-1 = 1<br /> result: jy1<br />Example 2:<br /> input1: 111 input2: bhanuchandra<br /> Output: “bucdr3”<br /> 111 is not an armstrong number<br /> remove duplicates occuring more than once (a, h, n will be removed)Anonymoushttps://www.blogger.com/profile/03041578849716760428noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-18603827306302521042019-08-19T01:58:27.656-07:002019-08-19T01:58:27.656-07:00write a program to remove duplicate string from a ...write a program to remove duplicate string from a sentence without using any predefined method.<br />Example : input :: abc hello is my abc is xyz<br /> output:: abc hello is my xyz<br />Ehtashamhttps://www.blogger.com/profile/00348779546234556775noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-51179946818193143242018-07-27T23:42:17.867-07:002018-07-27T23:42:17.867-07:00mayank, remove != replace with zero mayank, remove != replace with zero Trikihttps://www.blogger.com/profile/09747639784271946306noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-12598203825113971622018-07-27T08:31:13.748-07:002018-07-27T08:31:13.748-07:00i dont understand the loop conditions..
why not th...i dont understand the loop conditions..<br />why not this :<br /><br /> char[] array = input.toCharArray();<br /> for(int i=0; i<input.length();i++){<br /> for(int j=i+1;j<input.length();j++){<br /> if(array[i] == array[j]){<br /> array[j] = 0;<br /> }<br /> }<br /> }<br /><br />mayankhttps://www.blogger.com/profile/10950071881020624960noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-50054458667734214852016-07-22T00:42:50.831-07:002016-07-22T00:42:50.831-07:00@MyserY, theoretically, but for a large enough &qu...@MyserY, theoretically, but for a large enough "n" time of "removeDuplicatesV2" are better ...<br /><br />Therefore, the complexity analysis is not complete. Perhaps the code-statements used are not atomic ...Trikihttps://www.blogger.com/profile/09747639784271946306noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-26341031214057741972016-07-21T07:22:18.561-07:002016-07-21T07:22:18.561-07:00@Triki, that is not more efficient. if you see the...@Triki, that is not more efficient. if you see the implementation of indexOf:<br />public int indexOf(String str, int fromIndex) {<br /> return String.indexOf(value, 0, count,<br /> str.toCharArray(), 0, str.length(), fromIndex);<br /> }<br />you will see it is an Iterative solution, is O(n*n).<br />When you check in Set you will use a Hash and solution is OMyserYhttps://www.blogger.com/profile/01240489523903730249noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-65859856372763991152016-07-21T05:50:46.115-07:002016-07-21T05:50:46.115-07:00Ok, MyserY, more efficient:
public String rem...Ok, MyserY, more efficient:<br /><br /> public String removeDuplicatesV2(String word) {<br /> if (word == null || word.length() < 2) {<br /> return word;<br /> }<br /> StringBuilder sb = new StringBuilder(word.length());<br /> sb.append(word.charAt(0));<br /> <br /> for(int i = 1; i < word.length() ; ++i) {<br /> String Trikihttps://www.blogger.com/profile/09747639784271946306noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-56243731420482839732016-07-20T09:51:09.560-07:002016-07-20T09:51:09.560-07:00@Anonymous, look my solution is the same like you ...@Anonymous, look my solution is the same like you said. MyserYhttps://www.blogger.com/profile/01240489523903730249noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-78629104410441776732016-07-20T04:07:54.779-07:002016-07-20T04:07:54.779-07:00why not just add the items in a linkedhashmap/set ...why not just add the items in a linkedhashmap/set then turn that into an array? the multiples arent stored and it keeps orderAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-11253943626151264542016-06-23T08:47:31.406-07:002016-06-23T08:47:31.406-07:00@Triki if you see the documentation of distinct fo...@Triki if you see the documentation of distinct for that implementation can see that:<br />// While functional and quick to implement, this approach is not very efficient.<br /> // An efficient version requires an int-specific map/set implementation.<br /><br />Also i realy don't know if that will be more efficient, but yes is more simple ^^.<br />Gretings.MyserYhttps://www.blogger.com/profile/01240489523903730249noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-4859594751472804652016-06-23T08:36:43.613-07:002016-06-23T08:36:43.613-07:00i like more general option, if we don't have l...i like more general option, if we don't have limit of space complexity we can use some structure, i like for this problems HashMaps. my solution:<br />public static String removeDuplicates(String word) {<br /> if (word == null || word.length() <= 1) {<br /> return word;<br /> }<br /><br /> Set unduplicate = new HashSet<>(word.length());<br />MyserYhttps://www.blogger.com/profile/01240489523903730249noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-28620929496951663332016-06-17T19:51:43.306-07:002016-06-17T19:51:43.306-07:00@Triki, indeed the Java 8 solution is very simple ...@Triki, indeed the Java 8 solution is very simple with clever use of distinct() you can remove duplicate character from a character stream. Thanksjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-4491053396296667072016-06-17T02:28:14.040-07:002016-06-17T02:28:14.040-07:00Even simpler with Java 8 (in my opinion):
publi...Even simpler with Java 8 (in my opinion):<br /><br /><br /><br />public String removeDuplicatesJ8(String word) {<br /> if (word == null || word.length() < 2) {<br /> return word;<br /> }<br /> StringBuilder sb = new StringBuilder(word.length());<br /> word.chars().distinct()<br /> .mapToObj(ch -> (char)ch).forEach((ch) -> sb.append(ch));<br /> return Trikihttps://www.blogger.com/profile/09747639784271946306noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-67071843617195371572016-06-17T00:51:00.060-07:002016-06-17T00:51:00.060-07:00Simpler (in my opinion):
public String remov...Simpler (in my opinion):<br /><br /><em><br /> public String removeDuplicates(String literal) {<br /> if (literal == null || literal.length() < 2) {<br /> return literal;<br /> }<br /> StringBuilder sb = new StringBuilder(literal.length());<br /> char[] characters = literal.toCharArray();<br /> sb.append(Character.toString(characters[0]));<br />Trikihttps://www.blogger.com/profile/09747639784271946306noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-46519011594874595992016-06-11T02:50:42.498-07:002016-06-11T02:50:42.498-07:00Can't we code like that:
public static String...Can't we code like that:<br /><br />public static String removeDuplicates(String word) {<br />if (word == null || word.length() < 2) {<br />return word;<br />}<br />char[] characters = word.toCharArray();<br />for (int i = 1; i < word.length(); i++) {<br />int j;<br />for (j = 0; j < i; ++j) {<br />if (characters[i] == characters[j]) {<br />character[j] == 0;<br />}<br />}<br />}<br Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-32914660809314819802016-06-10T12:26:50.943-07:002016-06-10T12:26:50.943-07:00Why do we need to do an extra traversal just to re...Why do we need to do an extra traversal just to remove the null. What if we have 1M characters in the input string. We can avoid this extra traversal by building the string on the fly, i.e in the removeDuplicatesFromString method itself. Unknownhttps://www.blogger.com/profile/14740849477404415365noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-69518249009665739312016-06-10T04:57:50.795-07:002016-06-10T04:57:50.795-07:00Hello @Deepak and @Ashish, good catch. ThanksHello @Deepak and @Ashish, good catch. Thanksjavin paulhttps://www.blogger.com/profile/15028902221295732276noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-72002974309350347202016-06-10T01:49:43.748-07:002016-06-10T01:49:43.748-07:00In removeDuplicates method, pos variable is not re...In removeDuplicates method, pos variable is not required<br /><br />public static String removeDuplicates(String word) {<br /> if (word == null || word.length() < 2) {<br /> return word;<br /> }<br /> char[] characters = word.toCharArray();<br /> for (int i = 1; i < word.length(); i++) {<br /> int j;<br /> for (j = 0; j < i; ++j) {<br /> if (characters[i] == characters[j]) {Ashish Agarwalhttps://www.blogger.com/profile/06257361946396435667noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-19279437446413230462016-06-09T23:45:02.033-07:002016-06-09T23:45:02.033-07:00Variable pos is an extra variable.It's not req...Variable pos is an extra variable.It's not required.<br />Here is the removeDuplicates method without pos variable.<br /><br />public static String removeDuplicates(String word) {<br /> if (word == null || word.length() < 2) {<br /> return word;<br /> }<br /> int pos = 1; // possible position of duplicate character<br /> char[] characters = Deepak Kumarhttps://www.blogger.com/profile/13321580426971247690noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-23319948201986528182016-06-09T23:37:47.442-07:002016-06-09T23:37:47.442-07:00Please put the code samples in a github repo so th...Please put the code samples in a github repo so that we can try them easily.<br />Currently if I copy paste the code samples,there is no formatting at all and there is an embedded link everywhere "Read more: http://javarevisited.blogspot.com/2016/06/how-to-remove-duplicate-characters-from-String-Java.html#ixzz4B9fdcSug"Deepak Kumarhttps://www.blogger.com/profile/13321580426971247690noreply@blogger.comtag:blogger.com,1999:blog-8712770457197348465.post-33826884993540631282016-06-09T20:27:39.375-07:002016-06-09T20:27:39.375-07:00What is the time complexity of second solution usi...What is the time complexity of second solution using ASCII table? I guess its still O(n) right because you need to check for n characters in String and each check is O(1) because of array access? am I right? Any further way to improve this algorithm to find duplicate character let's say in log(N) or constant time?Anonymousnoreply@blogger.com