0

I am trying to find a Single Duplicate element in an array of integers nums containing n + 1 integers where each integer is in the range [1, n] inclusive. As its only single element so i did like this.

def findDuplicate(self, nums: [int]) ->:
    res1=0
    res2=0
    for i in range(1,len(nums)+1):
        res1^=i
    for i in range(0,len(nums)):
        res2^=nums[i]

    print(res1^res2)

I am using the input as [1,2,3,3,4] Instead of printing 3 its giving output as 6.

Ash3060
  • 188
  • 2
  • 15
  • 1
    does this answer your question https://stackoverflow.com/questions/10764286/using-xor-operator-for-finding-duplicate-elements-in-a-array-fails-in-many-cases ? – Arsenic Mar 28 '21 at 03:40
  • I have seen this already but i dont understand why mine is failing cause i have just used simple logic.Here the elements are starting from 1.In that question they have suggested to use min and max but here i dont need this as for my case min would be always 1 and max would be length of array-1 – Ash3060 Mar 28 '21 at 03:45
  • 1
    You are using `len(nums)` as if it is `n`, but it is actually `n + 1`. The first loop should be `for i in range(1, len(nums)):`. – kaya3 Mar 28 '21 at 03:47
  • Oops i didnt realise:(.I had even wriiten this in the comment above that max for me is array-1.Thanks:))) – Ash3060 Mar 28 '21 at 03:51

0 Answers0