Leetcode 287 Solution

This article provides solution to leetcode question 287 (find-the-duplicate-number)

https://leetcode.com/problems/find-the-duplicate-number

Solution

class Solution:
    def findDuplicate(self, nums: List[int]) -> int:
        fast = len(nums) - 1
        slow = len(nums) - 1

        while True:
            slow = nums[slow] - 1
            fast = nums[nums[fast] - 1] - 1

            if slow == fast:
                break

        slow = len(nums) - 1
        while fast != slow:
            slow = nums[slow] - 1
            fast = nums[fast] - 1

        return slow + 1