class Solution:
def findDisappearedNumbers(self, nums: List[int]) -> List[int]:
output=[]
for each in nums:
while each != nums[each-1]:
nums[each-1],temp=each,nums[each-1]
each=temp
for i in range(1,len(nums)+1):
if i != nums[i-1]:
output.append(i)
return outputThis way will work for array like [1,1,1,2,4,5,8]
The original problem only asked for array where some number appear once, some twice. This solution has no such limitation.
In short, you put 4 to nums[3] and if nums[3] was 7, you put that in nums[6], until finally every element is in place by this rule.
Whatever isn't in the final list is the missing ones.