+ 2

How will i correct my this code as my last word is not coming and also i want to remove my space???

i.e. i want the output as like this Output: vernda https://code.sololearn.com/cvjdD5Jjnz91/?ref=app Gaurav Agrawal Pro Coder Donna

2nd Jan 2019, 3:20 PM
Harsh Agrawal
Harsh Agrawal - avatar
13 Answers
+ 15
● doubt comments on code: 1)j<=l will cause IndexOutOfBound exception : as j will be ranging from (i+1) to l, that means for j=l, exception will occur. 2)replacing by '' will cause an error , so replaced by ' ' : U can make use of syntax : stringValue.replace(String,String); So U can use of : stringValue.replace(charValue+"", ""); //here we converted charValue to String by adding "". [also replacing by "" will change the length of s will cause IndexOutOfBound exceptions] 3)why "a" not printed: i moved from 0 to l-1, j moved from i+1 to l-1 but for i=l-1, inner loop having j will not work, that means j will be having value of previous iteration, that will be l-2+1=l-1 //so if() in outer loop [c!=c1] will be false & nothing will be printed. ●By now many solutions are available, but by considering your original code some fixes: 1)s+=" "; //adding 1 space to s to tackle 3rd pt. 2)(char==' '?""?char) in print stat. //still,there is need of improvement in original logic
2nd Jan 2019, 5:27 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 11
Michal I like your Streams approach much better than mine. I was wondering if there was a reduce method available. Now I know. 😉👍 Also, your method 3 with booleans was simple, yet clean and effective. Nicely done. 👏😎
2nd Jan 2019, 6:52 PM
David Carroll
David Carroll - avatar
+ 10
Rewa Mathur I further simplified the code you fixed. Harsh Agrawal See if you can identify the subtle changes made by Rewa, then by me https://code.sololearn.com/chbstUcNz7xl/?ref=app
2nd Jan 2019, 4:22 PM
David Carroll
David Carroll - avatar
+ 10
Michal I believe so. However, it wasn't clear to me either. 😉 Rewa Mathur Here's a version of the code using Java Streams. https://code.sololearn.com/csFU16d9EA0R/?ref=app I gotta admit, it's a bit ugly. But I'm quite biased towards LINQ in C#. đŸ€“
2nd Jan 2019, 4:59 PM
David Carroll
David Carroll - avatar
2nd Jan 2019, 6:34 PM
Michal
Michal - avatar
+ 5
Harsh Agrawal If you look a few comments up, Gaurav Agrawal posted a nice breakdown of the issues in your code.
3rd Jan 2019, 6:38 PM
David Carroll
David Carroll - avatar
+ 3
Idea of the code is to remove duplicates?
2nd Jan 2019, 3:35 PM
Michal
Michal - avatar
+ 1
Thnxzz David Carroll,Rewa Mathur ,Michal ,Gaurav Agrawal But how wll i correct my original code i.e. my code???
3rd Jan 2019, 4:05 PM
Harsh Agrawal
Harsh Agrawal - avatar
+ 1
Rewa Mathur No bro i want to say how wll i correct my original code??? This code is too different from my code!!!
3rd Jan 2019, 4:26 PM
Harsh Agrawal
Harsh Agrawal - avatar