beam: verify Floyd duplicate candidate occurs more than once

This commit is contained in:
beam authored and ledger committed 2026-10-27 13:29:41 +00:00
1 parent 1bbbd53d54
commit f400640b05
1 file changed
+6 -1
+6 -1
View File
@@ -33,7 +33,12 @@ def find_duplicate(nums: List[int]) -> int:
while slow2 != slow: while slow2 != slow:
slow2 = nums[slow2] slow2 = nums[slow2]
slow = nums[slow] 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: def _self_test() -> None: