We’re swapping the elements of the array only when they’re at not at correct indices using if nums[i] != nums[j]: conditional statement. If the element is at correct index, we simply move forward by incrementing the index i (i.e., we’re not swapping the elements here).
Here’s the coded solution that is being used in this lesson (with few additional printing statements here):
i = 0
while i < len(nums):
j = nums[i] - 1
if nums[i] != nums[j]:
nums[i], nums[j] = nums[j], nums[i] # swap
print("if : ", nums)
i += 1
missingNumbers = 
for i in range(len(nums)):
if nums[i] != i + 1:
missingNumbers.append(i + 1)
print(find_missing_numbers([2, 4, 1, 2]))
You can run this code to verify the swapping instances.
Hope this helps. If you still have any confusion, please let us know.