$a = [1,2,3,4,6,8,4,3,2,1]
def findtwonotrepeat(a,n)
$temp = 0
for i in 0...a.size
$temp ^= a[i] #两个只出现一次数字的异或结果保存在temp中
end
for j in 0...1.size
break if ($temp>>j)&1 == 1 #确定第j位为两数字的不同位
end
$pn = 0
$pm = 0
for i in 0...a.size
if (a[i]>>j)&1 == 1 #将数组分成两半,每一半都有一个只出现一次的数字
$pn ^= a[i]
else
$pm ^= a[i]
end
end
end
findtwonotrepeat($a, $a.size)
print $a
print "#{$pn} and #{$pm} are the two not repeat numbers in array.","\n"