I am trying to solve a leetcode problem, in which I have to find the number in a list that does not repeat. I have written this code, but it fails in this test case. I cannot seem to understand why it fails in this case. Can someone give me an explanation?
this is the code:
class Solution:
def singleNumber(self, nums: List[int]) -> int:
i = 0
while True:
if nums.count(nums[i]) == 1:
return nums[i]
break
else:
nums.remove(nums[i])
i = i 1
After passing 28 test cases it fails the case where nums = [1, 0, 1]
I have found another solution, so I am most interested in an explanation of why this code fails the given test case. Because I honestly don't understand it. Thanks.
CodePudding user response:
I'm surprised that this code passed 28 test cases without triggering the bug. The way that you use remove
is buggy, and also not needed. I don't see any good reason to mutate the list.
The core problem is that you seem to think that remove
removes all occurrences on an item. It doesn't -- it only removes the first occurrence.
With the test case that it fails, [1,0,1]
you start with i = 0
, hence you look at the first 1
. Its count is > 1, so you call the remove method with it. The resulting list is [0,1]
. At this pointi
is incremented to 1
, which is the index of 1
in the list. Since the first 1
has been removed, the count of the remaining 1
is 1. Your code thus returns 1
as the answer, rather than the correct answer of 0
.
As a general rule of thumb, it is a bad idea to mutate a list while iterating over it. The way that indices of items can shift because of the list mutation is a frequent source of bugs.