human
Write me a code to find the two repeating elements in a given array and the other steps required given the following:
Find the two repeating elements in a given array | Python3 code to Find the two repeating elements in a given array.
Will hold xor of all elements.
Will have only single set bit of xor.
Get the xor of all elements in arr [ ] and 1 , 2 . . n.
Get the rightmost set bit in set_bit_no.
Now divide elements in two sets by comparing rightmost set bit of xor with bit at same position in each element ..
XOR of first set in arr [ ].
XOR of second set in arr [ ].
XOR of first set in arr [ ] and 1 , 2 , ... n.
XOR of second set in arr [ ] and 1 , 2 , ... n.
Driver code"
Solution in Python:
assistant
#Here is code to find the two repeating elements in a given array and the other steps required.
 def printRepeating ( arr , size ) :
	xor = arr [ 0 ]
	n = size - 2
	x = 0
	y = 0
	for i in range ( 1 , size ) :
		xor ^= arr [ i ]
	for i in range ( 1 , n + 1 ) :
		xor ^= i
	set_bit_no = xor & ~ ( xor - 1 )
	for i in range ( 0 , size ) :
		if ( arr [ i ] & set_bit_no ) :
			x = x ^ arr [ i ]
		else :
			y = y ^ arr [ i ]
	for i in range ( 1 , n + 1 ) :
		if ( i & set_bit_no ) :
			x = x ^ i
		else :
			y = y ^ i
	print ( " The ▁ two ▁ repeating " , " elements ▁ are " , y , x )
arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ]
arr_size = len ( arr )
printRepeating ( arr , arr_size )