Find The Duplicate Number
Try to solve the Find the Duplicate Number problem.
Statement
Given an array of positive numbers, nums
, such that the values lie in the range , inclusive, and that there are numbers in the array, find and return the duplicate number present in nums
. There is only one repeated number in nums
, but it may appear more than once in the array.
Note: You cannot modify the given array
nums
. You have to solve the problem using only constant extra space.
Constraints:
nums.length
-
nums[i]
- All the integers in
nums
are unique, except for one integer that will appear more than once.
Examples
Understand the problem
Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:
Find The Duplicate Number
What is the output if the following list of integers is given as input?
[1, 5, 4, 3, 2, 4, 6]
5
3
4
7
Figure it out!
We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.
Try it yourself
Implement your solution in the following coding playground.
Need a nudge?
Explore these hints—each one is designed to guide you a step closer to the solution.
def find_duplicate(nums):# Replace this placeholder return statement with your codereturn 0