I am trying to solve the Set Mismatch problem using Cyclic Sort in Python. The problem statement is as follows: Problem Statement You have a set of integers s, which originally contains all numbers from 1 to n. Unfortunately, one of the numbers got duplicated, and another number is missing. Given an integer array nums representing the current state of the set, find the number that occurs twice and the missing number and return them as [duplicate, missing]. Examples Input: nums = [1,2,2,4] Output: [2,3] Input: nums = [1,1] Output: [1,2] My Current Solution I attempted to use Cyclic Sort to place numbers in their correct positions and identify the misplaced duplicate. However, my implementation enters an infinite loop for certain inputs like [2,3,2]. from typing import List def findErrorNums(nums: List[int]) -> List[int]: i = 0 n = len(nums) duplicated = 0 while i < n: print(f"i: {i}") print(f"Before: {i}, nums: {nums}") # If the number is in the right spot, skip it if i == nums - 1: i += 1 # If the target index already has the same number, it is a duplicate elif nums == nums[nums - 1]: duplicated = nums i += 1 # Send the number to its correct index else: nums, nums[nums - 1] = nums[nums - 1], nums print(f"After: {i}, nums: {nums}") print() # Identify the missing number for i in range(n): if i + 1 != nums: return [duplicated, i + 1] Issue For the input: nums = [2,3,2] The debug output shows an infinite loop where nums[0] keeps swapping between 2 and 3 without making progress. Debug Output (Repeating Pattern) i: 0 Before: 0, nums: [3, 2, 2] After: 0, nums: [2, 3, 2] i: 0 Before: 0, nums: [2, 3, 2] After: 0, nums: [3, 3, 2] i: 0 Before: 0, nums: [3, 3, 2] After: 0, nums: [2, 3, 2] i: 0 Before: 0, nums: [2, 3, 2] After: 0, nums: [3, 3, 2] ... (repeats indefinitely) I am unable to understand why 3 is being duplicated instead of being swapped. Is there a bug in my code that I am unable to see? Continue reading...