# Find the Duplicate Number

`Input: nums = [1,3,4,2,2]Output: 2`
`Input: nums = [3,1,3,4,2]Output: 3`
`Input: nums = [1,1]Output: 1`
`Input: nums = [1,1,2]Output: 1`
• `1 <= n <= 105`
• `nums.length == n + 1`
• `1 <= nums[i] <= n`
• All the integers in `nums` appear only once except for precisely one integer which appears two or more times

Some approaches that would solve the problem , but against the defined constraints

`    def findDuplicate(self, nums):             for i in range(len(nums)):            index=abs(nums[i])-1            if(nums[index]>0):                nums[index]*=-1            else:                return abs(nums[i])`
`class Solution(object):    def findDuplicate(self, nums):                low=1        high=len(nums)-1         def counter(nums,n):            cnt=0            for i in range(len(nums)):                if nums[i]<=n:                    cnt+=1                        return cnt        min_count=float('inf')        while(low<=high):            mid=(low+high)//2            counts=counter(nums,mid)            if counts>mid and min_count>=counts:                #print("/",[mid])                min_count=counts                val=mid                                            if(counts<=mid):                low=mid+1            else:                high=mid-1                        return val`
`    def findDuplicate(self, nums):        size=(len(nums)-1).bit_length()        val=0        for i in range(size):            mask=1<<i            nums_cnt=0            range_cnt=0            for j in nums:                if j&mask!=0:                    nums_cnt+=1              for k in range(1,len(nums)):                if k&mask!=0:                    range_cnt+=1            if(nums_cnt-range_cnt>0):               val|=mask        return val`
`class Solution(object):    def findDuplicate(self, nums):        slow=nums[0]        fast =nums[0]        while(True):            slow=nums[slow]            fast=nums[nums[fast]]            if(fast==slow):                break        slow=nums[0]        while(fast!=slow):            slow=nums[slow]            fast=nums[fast]        return slow`

--

--

--

## More from Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.

Love podcasts or audiobooks? Learn on the go with our new app.

## Dhanarajappu

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.