645. Set Mismatch

Input: nums = [1,2,2,4]
Output: [2,3]
Input: nums = [1,1]
Output: [1,2]
  • 2 <= nums.length <= 10^4
  • 1 <= nums[i] <= 10^4
class Solution:
def findErrorNums(self, nums: List[int]) -> List[int]:
xor_=0
miss,dup=0,0
hash=dict()
for i in range(len(nums)):
if nums[i] not in hash:
hash[nums[i]]=1
else:
hash[nums[i]]+=1
cnt=1
#cnt is number from 1 to len(nums)
for i in hash:
xor_^=i^cnt
cnt+=1
if(hash[i]==2):
dup=i
miss=xor_^cnt
return[dup,miss]
class Solution:
def findErrorNums(self, nums: List[int]) -> List[int]:
set_=set()
result=[]
xor_=0
miss,dup=0,0
for i in nums:
set_.add(i)

for i in range(len(nums)):
xor_^=(i+1)^nums[i]

for i in range(1,len(nums)+1):
if(i not in set_):
miss=i
dup=miss^xor_
return([dup,miss])
class Solution:
def findErrorNums(self, nums: List[int]) -> List[int]:
xor_=0
set_group_xor,unset_group_xor=0,0,
for i in range(len(nums)):
xor_^=(i+1)^nums[i]
right=xor_ & ~(xor_-1)
for i in range(1,len(nums)+1):
if(i&right!=0):
set_group_xor^=i
else:
unset_group_xor^=i
if(nums[i-1]&right!=0):
set_group_xor^=nums[i-1]
else:
unset_group_xor^=nums[i-1]


val1=xor_^set_group_xor
val2=xor_^unset_group_xor
for i in nums:
if(i==val1):
return(val1,val2)
return(val2,val1)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dhanarajappu

Dhanarajappu

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