Skip to content

Instantly share code, notes, and snippets.

@faheemsharif-me
Created September 19, 2023 23:05
Show Gist options
  • Select an option

  • Save faheemsharif-me/b513a3b059b9c6899d14684bd6d7ccf4 to your computer and use it in GitHub Desktop.

Select an option

Save faheemsharif-me/b513a3b059b9c6899d14684bd6d7ccf4 to your computer and use it in GitHub Desktop.
Find Duplicate Number in Python, Time Complexity 0(N), Space Complexity 0(N)
class Solution(object):
def findDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
for i in range(len(nums)):
if nums[abs(nums[i])] < 0:
return abs(nums[i])
nums[abs(nums[i])] = -nums[abs(nums[i])]
# Main Call
nums = [1,3,4,2,2]
solution = Solution()
print(solution.findDuplicate(nums))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment