+ 2

Better ways to do this

I did this CodeCoach Deja Vu challenge and i feel like my approach is a bit clumsy,so i wanted to know about better and more efficient approaches. https://code.sololearn.com/c9t9Ez2wHd9e/?ref=app

28th Jan 2020, 6:42 PM
Spandan Bhattacharya
Spandan Bhattacharya - avatar
12 Answers
+ 4
If you have already Solved it then need to further discussion here. If you are getting any problem you can ask.
28th Jan 2020, 6:45 PM
AÍąJ
AÍąJ - avatar
+ 3
With every i you only have to compare with the letters right from it (j=i+1).
28th Jan 2020, 7:05 PM
HonFu
HonFu - avatar
+ 2
I don't think you need to put "put(a)" at the end
3rd Nov 2020, 5:30 PM
Ahadjon Naimov
Ahadjon Naimov - avatar
+ 1
Ok thanks
28th Jan 2020, 6:46 PM
Spandan Bhattacharya
Spandan Bhattacharya - avatar
+ 1
I used strchr and strrchr (in a for loop) from the string.h header file and compare what get returned...if they are different... then "Deja Vu".
28th Jan 2020, 9:05 PM
rodwynnejones
rodwynnejones - avatar
+ 1
Hey, i think a better way is to create a array of char or string and sort it. If a string contains a duplicate than it will be set one by one gradually. Like "bacda" after sorting it becomes "aabcd". So it will be quite easy to find duplicate. And furthermore if you use Java than you have to try Hashset or LinkedHashset. Main use of this type of collections is it does not allow duplicates value.If you put a duplicate value in Hashset it will remove it. If you put "bacda" Hashset will return something like "bacd" if "bacda" ! ="bacd" //repeated else //norepeated
30th Jan 2020, 4:49 PM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 1
ok
31st Jan 2020, 10:25 AM
Spandan Bhattacharya
Spandan Bhattacharya - avatar
+ 1
Remove puts(a) 21st line then you get exact result
17th Dec 2020, 6:55 AM
pallavi
0
oo ok thnx
28th Jan 2020, 9:12 PM
Spandan Bhattacharya
Spandan Bhattacharya - avatar
0
i changed the double letter only problem,and now it is runs better,but your example will only search for repeats between 2 adjacent letters only.
28th Jan 2020, 11:32 PM
Spandan Bhattacharya
Spandan Bhattacharya - avatar
0
yeah, that totally isn't a good solution even though it worked. i do it this way, first create an int array of 26 size where all of it is set to 0 where each index represents an alphabet. ex arr[0] is for a, arr[1] for b... the array value will represent the amount of alphabet so when you loop through the string, and found an 'a' then arr[0] will add by 1. after adding 1 on the index check if the value is 2, which means there are 2 'a' s already, then print deja vu if you reach the end of the string then print unique turning the character 'a' into 0 involves the use of ascii numbers, i suggest you study it. so the ascii of a = 97, b = 98. you just need to decrease the ascii value by 97 and you will return the index to store on your array. this is how i solve it and in my opinion the simplest way.
29th Jan 2020, 3:38 PM
Shen Bapiro
Shen Bapiro - avatar
0
Yeah this is a nice method.
29th Jan 2020, 9:28 PM
Spandan Bhattacharya
Spandan Bhattacharya - avatar