LeetCode: Week 1 — Single Number
30-day leet coding challenge
Problem Statement :
Table of Contents:
Code Solution:
Time Complexity: O(n) | Space Complexity: O(1)
Explanation:
For this problem, we’ll be using Bitwise XOR to get the result. Let's understand how bitwise XOR works
here, [ A=0, B=0 & A=1, B =1] Both values of bit A and B are equal so it will result in 0
and [A=0, B=1 & A=1, B=0] both values of bit A and B are not equal which will result in 1
.
Using this technique, we’ll be finding a unique number in the array. In the XOR operation, the Bitwise method evaluates the binary representation of the given input value. as you can see that below with variable singleNum
and nums[i]
.
Let's understand the solution code with its execution values for each Iteration.
Run Code:
You can also check out other problem solution below
I hope you enjoy this solution. Let me know if you have any other solutions in response. Cheers :)