0
where am i wrong
function rem(w){ var a=w.split("") for(var i=0;i<a.length;i++){ for(var j=i+1;j<a.length;j++){ if(a[i]==a[j]){ a.splice(i,1) a.splice(j,1) } } } return a } console.log(rem('abcdabc')) i have made this program to display the character which isnt used more than once but it doesnt seem to be working.plz explain
2 Answers
+ 2
The splice() function works in place, so, when the if statement is entered and the first splice() ran the elements after are shifted. Now when the next line is ran the index j is off by 1. You can fix this issue by subtracting 1 from j in the splice call. Also, you will now need to move i back one spot or it will seem to jump forward due to the remaining elements shifting as well. Again this can be fixed by subtracting 1 from i so that the outer loop repeats the previous index it found a match on since that element has been deleted.
a.splice(i,1);
a.splice(j-1,1);
i--;
0
You did not use semicolon in first line I think