Home

Find the Duplicate

You are given an array of integers nums containing n + 1 integers where each integer is in the range [1, n] (inclusive). There is only one repeated number in nums. Return this number.

A simple solution is to sort the array and search for the duplicate. A more intuitive solution involves modifying nums in place by marking elements.

Challenge: Can you solve this in O(n) time without modifying nums and in O(1) space?

findTheDuplicate([1, 3, 4, 2, 2]) → 2
findTheDuplicate([3, 1, 3, 4, 2]) → 3
findTheDuplicate([[1, 1]]) → 1

😳 TIME LEFT: seconds 😳

function findTheDuplicate(nums) {

}

TOTAL TIME: seconds