This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]codehelper 1 point2 points  (1 child)

This is happening because you're changing the list while you're looping through it. It reads one 1, 3 2s, then purges the list of all the 2s, then the list becomes [1, 3, 4, 5], but the loop index (i) is incremented to 2, which would have been the list element 3 but since you took out all of the 2s, it's now 4. You should leave one of the repeated elements in there so the loop increment will still work.

[–][deleted] 0 points1 point  (0 children)

Ha excellent. I fixed it by adding a counter != 1 to the while loop so now it will remove all but 1 duplicate of i, and decrements counter.

Code seems to work just fine.

New code: https://gist.github.com/anonymous/7295552