#287
Find the Duplicate Number
medium· Linked Listruns: 0Given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive, there is exactly one repeated number. Return the repeated number. The algorithm must use only constant extra space and may not modify the input array.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 371
class Solution:
def findDuplicate(self, nums: list[int]) -> int:
slow = fast = nums[0]
while True:
slow = nums[slow]
fast = nums[nums[fast]]
if slow == fast:
break
slow2 = nums[0]
while slow != slow2:
slow = nums[slow]
slow2 = nums[slow2]
return slow
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.