Skip to contentKEEP THE CRAFTTHE SANCTUARY

Find the Duplicate Number

Find the one repeated value without changing the array or using extra memory.

The problem

Implement Solution.findDuplicate(nums). nums holds n + 1 integers, where n is between 1 and 100,000, and every value is between 1 and n. Exactly one value is repeated, possibly more than twice. Return that value. Do not change nums, and use only O(1) extra space, so a set or a sorted copy is not allowed.

Starter code

class Solution:
    def findDuplicate(self, nums):
        pass

Public examples

  1. nums = [1, 3, 4, 2, 2]gives2
  2. nums = [1, 1]gives1
  3. nums = [3, 3, 3, 3]gives3

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.