|
| 1 | +--- |
| 2 | +id: single-number |
| 3 | +title: Single Number |
| 4 | +sidebar_label: 0136 Single Number |
| 5 | +tags: |
| 6 | + - Java |
| 7 | + - Python |
| 8 | + - C++ |
| 9 | + - XOR |
| 10 | +description: "This is a solution to the Single Number problem on LeetCode." |
| 11 | +--- |
| 12 | + |
| 13 | +## Problem Description |
| 14 | + |
| 15 | +Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. |
| 16 | + |
| 17 | +You must implement a solution with a linear runtime complexity and use only constant extra space. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | + |
| 23 | +``` |
| 24 | +Input: nums = [2,2,1] |
| 25 | +Output: 1 |
| 26 | +
|
| 27 | +``` |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | + |
| 31 | +Input: nums = [4,1,2,1,2] |
| 32 | +Output: 4 |
| 33 | + |
| 34 | +**Example 3:** |
| 35 | + |
| 36 | +``` |
| 37 | +Input: nums = [1] |
| 38 | +Output: 1 |
| 39 | +``` |
| 40 | + |
| 41 | +### Constraints |
| 42 | + |
| 43 | +- $1 \leq nums.length \leq 3 * 10^4$ |
| 44 | +- $-3 \times 10^4 \leq nums[i] \leq 3 \times 10^4$ |
| 45 | +- Each element in the array appears twice except for one element which appears only once. |
| 46 | + |
| 47 | +--- |
| 48 | + |
| 49 | +## Solution for Single Number Problem |
| 50 | + |
| 51 | +### Intuition |
| 52 | + |
| 53 | +When faced with this problem, the first hurdle to overcome is the strict limitations imposed on the solution: linear time complexity and constant space complexity. This means traditional methods of identifying duplicates like hash tables or sorting won't cut it. Instead, we'll need to use a different kind of magic, one rooted in the world of bitwise operations! |
| 54 | + |
| 55 | +### Approach |
| 56 | + |
| 57 | +Our saviour here is the XOR operation. XOR stands for 'exclusive or', and the magic lies in its properties. When a number is XORed with itself, the result is 0. And when a number is XORed with 0, the result is the number itself. |
| 58 | + |
| 59 | +So, if we XOR all the numbers in the array, all the numbers appearing twice will cancel each other out and we'll be left with the single number that appears only once. |
| 60 | + |
| 61 | +#### Code in Different Languages |
| 62 | + |
| 63 | +<Tabs> |
| 64 | + <TabItem value="Java" label="Java"> |
| 65 | + <SolutionAuthor name="@vanAmsen"/> |
| 66 | + ```java |
| 67 | + class Solution { |
| 68 | + public int singleNumber(int[] nums) { |
| 69 | + int result = 0; |
| 70 | + for (int num : nums) { |
| 71 | + result ^= num; |
| 72 | + } |
| 73 | + return result; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +````` |
| 78 | +</TabItem> |
| 79 | +<TabItem value="Python" label="Python"> |
| 80 | +<SolutionAuthor name="@vanAmsen"/> |
| 81 | + |
| 82 | +````python |
| 83 | +class Solution: |
| 84 | + def singleNumber(self, nums: List[int]) -> int: |
| 85 | + result = 0 |
| 86 | + for num in nums: |
| 87 | + result ^= num |
| 88 | + return result |
| 89 | + |
| 90 | +````` |
| 91 | + |
| 92 | +</TabItem> |
| 93 | +<TabItem value="C++" label="C++"> |
| 94 | +<SolutionAuthor name="@vanAmsen"/> |
| 95 | +```cpp |
| 96 | +class Solution { |
| 97 | +public: |
| 98 | + int singleNumber(vector<int>& nums) { |
| 99 | + int result = 0; |
| 100 | + for (int num : nums) { |
| 101 | + result ^= num; |
| 102 | + } |
| 103 | + return result; |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +``` |
| 108 | +</TabItem> |
| 109 | +</Tabs> |
| 110 | + |
| 111 | +#### Complexity Analysis |
| 112 | + |
| 113 | +- Time complexity: $O(n)$, as we're iterating over the array only once. |
| 114 | +- Space complexity: $O(1)$, because we're only using a single variable to store the result, irrespective of the size of the input. |
| 115 | + |
| 116 | +## References |
| 117 | + |
| 118 | +- **LeetCode Problem:** [Single Number](https://leetcode.com/problems/single-number/description/) |
| 119 | +- **Solution Link:** [Single NUmber on LeetCode](https://leetcode.com/problems/single-number/solutions/3801367/video-single-number-a-bitwise-magic-trick/) |
| 120 | +- **Authors Leetcode Profile:** [vanAmsen](https://leetcode.com/u/vanAmsen/) |
| 121 | +``` |
0 commit comments