14. Non Repeating Numbers
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
To solve this problem, I utilized bitwise XOR operations to find the non-repeating numbers. Here's the breakdown of my approach:
I started by XORing all the elements in the input array. Since XOR of two same values results in 0
, this operation effectively gives me the XOR value of the two unpaired numbers.
I then created a bitmask by finding the rightmost set bit in the XOR value. This was done through bit manipulation, where I shifted the mask to the left until the XOR value's rightmost bit was set.
Using this bitmask, I looped through the input array again. For each number, I checked whether the corresponding bit in the bitmask was set or unset. Based on this, I separated the numbers into two groups: those with the bit set and those with the bit unset.
Finally, I XORed all the numbers in each group separately to find the two non-repeating numbers.
Consider the input array [1, 2, 3, 2, 1, 4]
.
XORing all elements gives 3 ^ 4
, which is 7
(binary 111
).
Then, I find the rightmost set bit, which is at the first position (from the right) in the binary representation of 7
. This gives me the bitmask 001
.
Now I separate the elements based on the bitmask:
Elements with set bit (001
): [1, 3, 1]
Elements with unset bit: [2, 2, 4]
After XORing the elements in each group:
XOR of elements with set bit: 1 ^ 3 ^ 1 = 3
XOR of elements with unset bit: 2 ^ 2 ^ 4 = 4
So, the two non-repeating numbers are 3
and 4
.
Time Complexity: The algorithm iterates through the input array twice. Both iterations are linear, so the overall time complexity is O(n)
.
Auxiliary Space Complexity: The algorithm uses a constant amount of extra space for variables and containers. Hence, the auxiliary space complexity is O(1)
.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star to the repository.