diff --git a/wizard-work/beam/find_duplicate.py b/wizard-work/beam/find_duplicate.py index a39bee0..ceefac4 100644 --- a/wizard-work/beam/find_duplicate.py +++ b/wizard-work/beam/find_duplicate.py @@ -33,7 +33,12 @@ def find_duplicate(nums: List[int]) -> int: while slow2 != slow: slow2 = nums[slow2] slow = nums[slow] - return slow + candidate = slow + # Verify the candidate actually occurs more than once; a permutation + # (no duplicate) also has cycles, so Floyd alone is not sufficient. + if nums.count(candidate) < 2: + raise ValueError("no duplicate present in nums") + return candidate def _self_test() -> None: